2d3c799c070d2ae3fda831b2b1fbf7f688918a60
[picodrive.git] / platform / common / mp3_libavcodec.c
1 /*
2  * Some mp3 related code for Sega/Mega CD.
3  * Uses Libav/FFmpeg libavcodec
4  * (C) notaz, 2013
5  *
6  * This work is licensed under the terms of MAME license.
7  * See COPYING file in the top-level directory.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <libavcodec/avcodec.h>
13 #include <dlfcn.h>
14
15 #include <pico/pico_int.h>
16 #include "../libpicofe/lprintf.h"
17 #include "mp3.h"
18
19 static AVCodecContext *ctx;
20
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);
26
27 int mp3dec_decode(FILE *f, int *file_pos, int file_len)
28 {
29         unsigned char input_buf[2 * 1024];
30         int frame_size;
31         AVPacket avpkt;
32         int bytes_in;
33         int bytes_out;
34         int offset;
35         int len;
36
37         p_av_init_packet(&avpkt);
38
39         do
40         {
41                 if (*file_pos >= file_len)
42                         return 1; // EOF, nothing to do
43
44                 fseek(f, *file_pos, SEEK_SET);
45                 bytes_in = fread(input_buf, 1, sizeof(input_buf), f);
46
47                 offset = mp3_find_sync_word(input_buf, bytes_in);
48                 if (offset < 0) {
49                         lprintf("find_sync_word (%i/%i) err %i\n",
50                                 *file_pos, file_len, offset);
51                         *file_pos = file_len;
52                         return 1; // EOF
53                 }
54
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;
60
61                 if (offset > 0 && bytes_in - offset < frame_size) {
62                         // underflow
63                         *file_pos += offset;
64                         continue;
65                 }
66
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;
74 #endif
75
76                 len = p_avcodec_decode_audio3(ctx, cdda_out_buffer,
77                         &bytes_out, &avpkt);
78                 if (len <= 0) {
79                         lprintf("mp3 decode err (%i/%i) %i\n",
80                                 *file_pos, file_len, len);
81
82                         // attempt to skip the offending frame..
83                         *file_pos += offset + 1;
84                         continue;
85                 }
86
87                 *file_pos += offset + len;
88         }
89         while (0);
90
91         return 0;
92 }
93
94 int mp3dec_start(FILE *f, int fpos_start)
95 {
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);
101         AVCodec *codec;
102         void *soh;
103         int ret;
104
105         if (ctx != NULL)
106                 return 0;
107
108         // either v52 or v53 should be ok
109         soh = dlopen("libavcodec.so.52", RTLD_NOW);
110         if (soh == NULL)
111                 soh = dlopen("libavcodec.so.53", RTLD_NOW);
112         if (soh == NULL) {
113                 lprintf("mp3dec: load libavcodec.so: %s\n", dlerror());
114                 return -1;
115         }
116
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");
124
125         if (avcodec_register_all == NULL || avcodec_find_decoder == NULL
126             || avcodec_alloc_context == NULL || avcodec_open == NULL
127             || av_free == NULL
128             || p_av_init_packet == NULL || p_avcodec_decode_audio3 == NULL)
129         {
130                 lprintf("mp3dec: missing symbol(s) in libavcodec.so\n");
131                 dlclose(soh);
132                 return -1;
133         }
134
135         // init decoder
136
137         //avcodec_init();
138         avcodec_register_all();
139
140         // AV_CODEC_ID_MP3 ?
141         codec = avcodec_find_decoder(CODEC_ID_MP3);
142         if (codec == NULL) {
143                 lprintf("mp3dec: codec missing\n");
144                 return -1;
145         }
146
147         ctx = avcodec_alloc_context();
148         if (ctx == NULL) {
149                 lprintf("mp3dec: avcodec_alloc_context failed\n");
150                 return -1;
151         }
152
153         ret = avcodec_open(ctx, codec);
154         if (ret < 0) {
155                 lprintf("mp3dec: avcodec_open failed: %d\n", ret);
156                 av_free(ctx);
157                 ctx = NULL;
158                 return -1;
159         }
160
161         return 0;
162 }