support mp3 decoding over libavcodec
[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
14 #include <pico/pico_int.h>
15 #include "../libpicofe/lprintf.h"
16 #include "mp3.h"
17
18 static AVCodecContext *ctx;
19
20 int mp3dec_decode(FILE *f, int *file_pos, int file_len)
21 {
22         unsigned char input_buf[2 * 1024];
23         int frame_size;
24         AVPacket avpkt;
25         int bytes_in;
26         int bytes_out;
27         int offset;
28         int len;
29
30         av_init_packet(&avpkt);
31
32         do
33         {
34                 if (*file_pos >= file_len)
35                         return 1; // EOF, nothing to do
36
37                 fseek(f, *file_pos, SEEK_SET);
38                 bytes_in = fread(input_buf, 1, sizeof(input_buf), f);
39
40                 offset = mp3_find_sync_word(input_buf, bytes_in);
41                 if (offset < 0) {
42                         lprintf("find_sync_word (%i/%i) err %i\n",
43                                 *file_pos, file_len, offset);
44                         *file_pos = file_len;
45                         return 1; // EOF
46                 }
47
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;
53
54                 if (offset > 0 && bytes_in - offset < frame_size) {
55                         // underflow
56                         *file_pos += offset;
57                         continue;
58                 }
59
60                 avpkt.data = input_buf + offset;
61                 avpkt.size = frame_size;
62                 bytes_out = sizeof(cdda_out_buffer);
63
64                 len = avcodec_decode_audio3(ctx, cdda_out_buffer,
65                         &bytes_out, &avpkt);
66                 if (len <= 0) {
67                         lprintf("mp3 decode err (%i/%i) %i\n",
68                                 *file_pos, file_len, len);
69
70                         // attempt to skip the offending frame..
71                         *file_pos += offset + 1;
72                         continue;
73                 }
74
75                 *file_pos += offset + len;
76         }
77         while (0);
78
79         return 0;
80 }
81
82 int mp3dec_start(void)
83 {
84         AVCodec *codec;
85         int ret;
86
87         if (ctx != NULL)
88                 return 0;
89
90         // init decoder
91
92         //avcodec_init();
93         avcodec_register_all();
94
95         // AV_CODEC_ID_MP3 ?
96         codec = avcodec_find_decoder(CODEC_ID_MP3);
97         if (codec == NULL) {
98                 lprintf("mp3dec: codec missing\n");
99                 return -1;
100         }
101
102         ctx = avcodec_alloc_context();
103         if (ctx == NULL) {
104                 lprintf("mp3dec: avcodec_alloc_context failed\n");
105                 return -1;
106         }
107
108         ret = avcodec_open(ctx, codec);
109         if (ret < 0) {
110                 lprintf("mp3dec: avcodec_open failed: %d\n", ret);
111                 av_free(ctx);
112                 ctx = NULL;
113                 return -1;
114         }
115
116         return 0;
117 }