2
0

mpegaudio_parser.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * MPEG Audio parser
  3. * Copyright (c) 2003 Fabrice Bellard
  4. * Copyright (c) 2003 Michael Niedermayer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "parser.h"
  23. #include "mpegaudiodecheader.h"
  24. #include "libavutil/common.h"
  25. #include "libavformat/apetag.h" // for APE tag.
  26. #include "libavformat/id3v1.h" // for ID3v1_TAG_SIZE
  27. typedef struct MpegAudioParseContext {
  28. ParseContext pc;
  29. int frame_size;
  30. uint32_t header;
  31. int header_count;
  32. int no_bitrate;
  33. } MpegAudioParseContext;
  34. #define MPA_HEADER_SIZE 4
  35. /* header + layer + freq + lsf/mpeg25 */
  36. #define SAME_HEADER_MASK \
  37. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  38. static int mpegaudio_parse(AVCodecParserContext *s1,
  39. AVCodecContext *avctx,
  40. const uint8_t **poutbuf, int *poutbuf_size,
  41. const uint8_t *buf, int buf_size)
  42. {
  43. MpegAudioParseContext *s = s1->priv_data;
  44. ParseContext *pc = &s->pc;
  45. uint32_t state= pc->state;
  46. int i;
  47. int next= END_NOT_FOUND;
  48. int flush = !buf_size;
  49. for(i=0; i<buf_size; ){
  50. if(s->frame_size){
  51. int inc= FFMIN(buf_size - i, s->frame_size);
  52. i += inc;
  53. s->frame_size -= inc;
  54. state = 0;
  55. if(!s->frame_size){
  56. next= i;
  57. break;
  58. }
  59. }else{
  60. while(i<buf_size){
  61. int ret, sr, channels, bit_rate, frame_size;
  62. enum AVCodecID codec_id = avctx->codec_id;
  63. state= (state<<8) + buf[i++];
  64. ret = ff_mpa_decode_header(state, &sr, &channels, &frame_size, &bit_rate, &codec_id);
  65. if (ret < 4) {
  66. if (i > 4)
  67. s->header_count = -2;
  68. } else {
  69. int header_threshold = avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec_id;
  70. if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  71. s->header_count= -3;
  72. s->header= state;
  73. s->header_count++;
  74. s->frame_size = ret-4;
  75. if (s->header_count > header_threshold) {
  76. avctx->sample_rate= sr;
  77. avctx->channels = channels;
  78. s1->duration = frame_size;
  79. avctx->codec_id = codec_id;
  80. if (s->no_bitrate || !avctx->bit_rate) {
  81. s->no_bitrate = 1;
  82. avctx->bit_rate += (bit_rate - avctx->bit_rate) / (s->header_count - header_threshold);
  83. }
  84. }
  85. if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  86. s->frame_size = 0;
  87. next = buf_size;
  88. } else if (codec_id == AV_CODEC_ID_MP3ADU) {
  89. avpriv_report_missing_feature(avctx,
  90. "MP3ADU full parser");
  91. *poutbuf = NULL;
  92. *poutbuf_size = 0;
  93. return buf_size; /* parsers must not return error codes */
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. pc->state= state;
  101. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  102. *poutbuf = NULL;
  103. *poutbuf_size = 0;
  104. return buf_size;
  105. }
  106. if (flush && buf_size >= ID3v1_TAG_SIZE && memcmp(buf, "TAG", 3) == 0) {
  107. *poutbuf = NULL;
  108. *poutbuf_size = 0;
  109. return next;
  110. }
  111. if (flush && buf_size >= APE_TAG_FOOTER_BYTES && memcmp(buf, APE_TAG_PREAMBLE, 8) == 0) {
  112. *poutbuf = NULL;
  113. *poutbuf_size = 0;
  114. return next;
  115. }
  116. *poutbuf = buf;
  117. *poutbuf_size = buf_size;
  118. return next;
  119. }
  120. AVCodecParser ff_mpegaudio_parser = {
  121. .codec_ids = { AV_CODEC_ID_MP1, AV_CODEC_ID_MP2, AV_CODEC_ID_MP3, AV_CODEC_ID_MP3ADU },
  122. .priv_data_size = sizeof(MpegAudioParseContext),
  123. .parser_parse = mpegaudio_parse,
  124. .parser_close = ff_parse_close,
  125. };