2 * Some mp3 related code for Sega/Mega CD.
3 * Uses Libav/FFmpeg libavcodec
6 * This work is licensed under the terms of MAME license.
7 * See COPYING file in the top-level directory.
12 #include <libavcodec/avcodec.h>
14 #include <pico/pico_int.h>
15 #include "../libpicofe/lprintf.h"
18 static AVCodecContext *ctx;
20 int mp3dec_decode(FILE *f, int *file_pos, int file_len)
22 unsigned char input_buf[2 * 1024];
30 av_init_packet(&avpkt);
34 if (*file_pos >= file_len)
35 return 1; // EOF, nothing to do
37 fseek(f, *file_pos, SEEK_SET);
38 bytes_in = fread(input_buf, 1, sizeof(input_buf), f);
40 offset = mp3_find_sync_word(input_buf, bytes_in);
42 lprintf("find_sync_word (%i/%i) err %i\n",
43 *file_pos, file_len, offset);
48 // to avoid being flooded with "incorrect frame size" errors,
49 // we must calculate and pass exact frame size - lame
50 frame_size = mpeg1_l3_bitrates[input_buf[offset + 2] >> 4];
51 frame_size = frame_size * 144000 / 44100;
52 frame_size += (input_buf[offset + 2] >> 1) & 1;
54 if (offset > 0 && bytes_in - offset < frame_size) {
60 avpkt.data = input_buf + offset;
61 avpkt.size = frame_size;
62 bytes_out = sizeof(cdda_out_buffer);
64 len = avcodec_decode_audio3(ctx, cdda_out_buffer,
67 lprintf("mp3 decode err (%i/%i) %i\n",
68 *file_pos, file_len, len);
70 // attempt to skip the offending frame..
71 *file_pos += offset + 1;
75 *file_pos += offset + len;
82 int mp3dec_start(void)
93 avcodec_register_all();
96 codec = avcodec_find_decoder(CODEC_ID_MP3);
98 lprintf("mp3dec: codec missing\n");
102 ctx = avcodec_alloc_context();
104 lprintf("mp3dec: avcodec_alloc_context failed\n");
108 ret = avcodec_open(ctx, codec);
110 lprintf("mp3dec: avcodec_open failed: %d\n", ret);