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>
15 #include <pico/pico_int.h>
16 #include "../libpicofe/lprintf.h"
19 static AVCodecContext *ctx;
21 /* avoid compile time linking to libavcodec due to huge list of it's deps..
22 * we also use this old API as newer one is not available on pandora */
23 void (*p_av_init_packet)(AVPacket *pkt);
24 int (*p_avcodec_decode_audio3)(AVCodecContext *avctx, int16_t *samples,
25 int *frame_size_ptr, AVPacket *avpkt);
27 int mp3dec_decode(FILE *f, int *file_pos, int file_len)
29 unsigned char input_buf[2 * 1024];
37 p_av_init_packet(&avpkt);
41 if (*file_pos >= file_len)
42 return 1; // EOF, nothing to do
44 fseek(f, *file_pos, SEEK_SET);
45 bytes_in = fread(input_buf, 1, sizeof(input_buf), f);
47 offset = mp3_find_sync_word(input_buf, bytes_in);
49 lprintf("find_sync_word (%i/%i) err %i\n",
50 *file_pos, file_len, offset);
55 // to avoid being flooded with "incorrect frame size" errors,
56 // we must calculate and pass exact frame size - lame
57 frame_size = mpeg1_l3_bitrates[input_buf[offset + 2] >> 4];
58 frame_size = frame_size * 144000 / 44100;
59 frame_size += (input_buf[offset + 2] >> 1) & 1;
61 if (offset > 0 && bytes_in - offset < frame_size) {
67 avpkt.data = input_buf + offset;
68 avpkt.size = frame_size;
69 bytes_out = sizeof(cdda_out_buffer);
70 #if LIBAVCODEC_VERSION_MAJOR < 53
71 // stupidity in v52: enforces this size even when
72 // it doesn't need/use that much at all
73 bytes_out = AVCODEC_MAX_AUDIO_FRAME_SIZE;
76 len = p_avcodec_decode_audio3(ctx, cdda_out_buffer,
79 lprintf("mp3 decode err (%i/%i) %i\n",
80 *file_pos, file_len, len);
82 // attempt to skip the offending frame..
83 *file_pos += offset + 1;
87 *file_pos += offset + len;
94 int mp3dec_start(FILE *f, int fpos_start)
96 void (*avcodec_register_all)(void);
97 AVCodec *(*avcodec_find_decoder)(enum CodecID id);
98 AVCodecContext *(*avcodec_alloc_context)(void);
99 int (*avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
100 void (*av_free)(void *ptr);
108 // either v52 or v53 should be ok
109 soh = dlopen("libavcodec.so.52", RTLD_NOW);
111 soh = dlopen("libavcodec.so.53", RTLD_NOW);
113 lprintf("mp3dec: load libavcodec.so: %s\n", dlerror());
117 avcodec_register_all = dlsym(soh, "avcodec_register_all");
118 avcodec_find_decoder = dlsym(soh, "avcodec_find_decoder");
119 avcodec_alloc_context = dlsym(soh, "avcodec_alloc_context");
120 avcodec_open = dlsym(soh, "avcodec_open");
121 av_free = dlsym(soh, "av_free");
122 p_av_init_packet = dlsym(soh, "av_init_packet");
123 p_avcodec_decode_audio3 = dlsym(soh, "avcodec_decode_audio3");
125 if (avcodec_register_all == NULL || avcodec_find_decoder == NULL
126 || avcodec_alloc_context == NULL || avcodec_open == NULL
128 || p_av_init_packet == NULL || p_avcodec_decode_audio3 == NULL)
130 lprintf("mp3dec: missing symbol(s) in libavcodec.so\n");
138 avcodec_register_all();
141 codec = avcodec_find_decoder(CODEC_ID_MP3);
143 lprintf("mp3dec: codec missing\n");
147 ctx = avcodec_alloc_context();
149 lprintf("mp3dec: avcodec_alloc_context failed\n");
153 ret = avcodec_open(ctx, codec);
155 lprintf("mp3dec: avcodec_open failed: %d\n", ret);