mp3_libavcodec.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <stdio.h>
  10. #include <string.h>
  11. #include <libavcodec/avcodec.h>
  12. #include <dlfcn.h>
  13. #include <pico/pico_int.h>
  14. #include "mp3.h"
  15. #if LIBAVCODEC_VERSION_MAJOR < 55
  16. #define AVCodecID CodecID
  17. #define AV_CODEC_ID_MP3 CODEC_ID_MP3
  18. #define AV_CH_LAYOUT_STEREO CH_LAYOUT_STEREO
  19. #define AV_SAMPLE_FMT_S16 SAMPLE_FMT_S16
  20. #define request_sample_fmt sample_fmt
  21. #endif
  22. static void *libavcodec;
  23. static AVCodecContext *ctx;
  24. /* avoid compile time linking to libavcodec due to huge list of it's deps..
  25. * we also use this old API as newer one is not available on pandora */
  26. void (*p_av_init_packet)(AVPacket *pkt);
  27. int (*p_avcodec_decode_audio3)(AVCodecContext *avctx, int16_t *samples,
  28. int *frame_size_ptr, AVPacket *avpkt);
  29. int mp3dec_decode(FILE *f, int *file_pos, int file_len)
  30. {
  31. unsigned char input_buf[2 * 1024];
  32. int frame_size;
  33. AVPacket avpkt;
  34. int bytes_in;
  35. int bytes_out;
  36. int offset;
  37. int len = -1;
  38. int retry = 3;
  39. p_av_init_packet(&avpkt);
  40. do
  41. {
  42. if (*file_pos >= file_len)
  43. return 1; // EOF, nothing to do
  44. fseek(f, *file_pos, SEEK_SET);
  45. bytes_in = fread(input_buf, 1, sizeof(input_buf), f);
  46. offset = mp3_find_sync_word(input_buf, bytes_in);
  47. if (offset < 0) {
  48. lprintf("find_sync_word (%i/%i) err %i\n",
  49. *file_pos, file_len, offset);
  50. *file_pos = file_len;
  51. return 1; // EOF
  52. }
  53. *file_pos += offset;
  54. // to avoid being flooded with "incorrect frame size" errors,
  55. // we must calculate and pass exact frame size - lame
  56. frame_size = mpeg1_l3_bitrates[input_buf[offset + 2] >> 4];
  57. frame_size = frame_size * 144000 / 44100;
  58. frame_size += (input_buf[offset + 2] >> 1) & 1;
  59. if (offset > 0 && bytes_in - offset < frame_size) {
  60. // underflow
  61. continue;
  62. }
  63. avpkt.data = input_buf + offset;
  64. avpkt.size = frame_size;
  65. bytes_out = sizeof(cdda_out_buffer);
  66. #if LIBAVCODEC_VERSION_MAJOR < 53
  67. // stupidity in v52: enforces this size even when
  68. // it doesn't need/use that much at all
  69. bytes_out = AVCODEC_MAX_AUDIO_FRAME_SIZE;
  70. #endif
  71. len = p_avcodec_decode_audio3(ctx, cdda_out_buffer,
  72. &bytes_out, &avpkt);
  73. if (len <= 0) {
  74. lprintf("mp3 decode err (%i/%i) %i\n",
  75. *file_pos, file_len, len);
  76. // attempt to skip the offending frame..
  77. *file_pos += 1;
  78. } else
  79. *file_pos += len;
  80. }
  81. while (len <= 0 && --retry > 0);
  82. return len <= 0;
  83. }
  84. int mp3dec_start(FILE *f, int fpos_start)
  85. {
  86. void (*avcodec_register_all)(void);
  87. AVCodec *(*avcodec_find_decoder)(enum AVCodecID id);
  88. #if LIBAVCODEC_VERSION_MAJOR < 54
  89. AVCodecContext *(*avcodec_alloc_context)(void);
  90. int (*avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
  91. #else
  92. AVCodecContext *(*avcodec_alloc_context)(AVCodec *);
  93. int (*avcodec_open)(AVCodecContext *avctx, AVCodec *codec, AVDictionary **);
  94. #endif
  95. void (*av_free)(void *ptr);
  96. AVCodec *codec;
  97. int ret;
  98. if (ctx != NULL)
  99. return 0;
  100. #if LIBAVCODEC_VERSION_MAJOR < 54
  101. // either v52 or v53 should be ok
  102. if (libavcodec == NULL)
  103. libavcodec = dlopen("libavcodec.so.52", RTLD_NOW);
  104. if (libavcodec == NULL)
  105. libavcodec = dlopen("libavcodec.so.53", RTLD_NOW);
  106. #else
  107. if (libavcodec == NULL)
  108. libavcodec = dlopen("libavcodec.so", RTLD_NOW);
  109. #endif
  110. if (libavcodec == NULL) {
  111. lprintf("mp3dec: load libavcodec.so: %s\n", dlerror());
  112. return -1;
  113. }
  114. avcodec_register_all = dlsym(libavcodec, "avcodec_register_all");
  115. avcodec_find_decoder = dlsym(libavcodec, "avcodec_find_decoder");
  116. #if LIBAVCODEC_VERSION_MAJOR < 54
  117. avcodec_alloc_context = dlsym(libavcodec, "avcodec_alloc_context");
  118. avcodec_open = dlsym(libavcodec, "avcodec_open");
  119. #else
  120. avcodec_alloc_context = dlsym(libavcodec, "avcodec_alloc_context3");
  121. avcodec_open = dlsym(libavcodec, "avcodec_open2");
  122. #endif
  123. av_free = dlsym(libavcodec, "av_free");
  124. p_av_init_packet = dlsym(libavcodec, "av_init_packet");
  125. p_avcodec_decode_audio3 = dlsym(libavcodec, "avcodec_decode_audio3");
  126. if (avcodec_register_all == NULL || avcodec_find_decoder == NULL
  127. || avcodec_alloc_context == NULL || avcodec_open == NULL
  128. || av_free == NULL
  129. || p_av_init_packet == NULL || p_avcodec_decode_audio3 == NULL)
  130. {
  131. lprintf("mp3dec: missing symbol(s) in libavcodec.so\n");
  132. return -1;
  133. }
  134. // init decoder
  135. //avcodec_init();
  136. avcodec_register_all();
  137. codec = avcodec_find_decoder(AV_CODEC_ID_MP3);
  138. if (codec == NULL) {
  139. lprintf("mp3dec: codec missing\n");
  140. return -1;
  141. }
  142. #if LIBAVCODEC_VERSION_MAJOR < 54
  143. ctx = avcodec_alloc_context();
  144. if (ctx == NULL) {
  145. lprintf("mp3dec: avcodec_alloc_context failed\n");
  146. return -1;
  147. }
  148. #else
  149. ctx = avcodec_alloc_context(codec);
  150. if (ctx == NULL) {
  151. lprintf("mp3dec: avcodec_alloc_context failed\n");
  152. return -1;
  153. }
  154. #endif
  155. ctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
  156. ctx->request_sample_fmt = AV_SAMPLE_FMT_S16;
  157. ctx->sample_rate = 44100;
  158. #if LIBAVCODEC_VERSION_MAJOR < 54
  159. ret = avcodec_open(ctx, codec);
  160. if (ret < 0) {
  161. lprintf("mp3dec: avcodec_open failed: %d\n", ret);
  162. av_free(ctx);
  163. ctx = NULL;
  164. return -1;
  165. }
  166. #else
  167. ret = avcodec_open(ctx, codec, NULL);
  168. if (ret < 0) {
  169. lprintf("mp3dec: avcodec_open failed: %d\n", ret);
  170. av_free(ctx);
  171. ctx = NULL;
  172. return -1;
  173. }
  174. #endif
  175. return 0;
  176. }