You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.4KB

  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/id3v1.h" // for ID3v1_TAG_SIZE
  26. typedef struct MpegAudioParseContext {
  27. ParseContext pc;
  28. int frame_size;
  29. uint32_t header;
  30. int header_count;
  31. int no_bitrate;
  32. } MpegAudioParseContext;
  33. #define MPA_HEADER_SIZE 4
  34. /* header + layer + bitrate + freq + lsf/mpeg25 */
  35. #define SAME_HEADER_MASK \
  36. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  37. static int mpegaudio_parse(AVCodecParserContext *s1,
  38. AVCodecContext *avctx,
  39. const uint8_t **poutbuf, int *poutbuf_size,
  40. const uint8_t *buf, int buf_size)
  41. {
  42. MpegAudioParseContext *s = s1->priv_data;
  43. ParseContext *pc = &s->pc;
  44. uint32_t state= pc->state;
  45. int i;
  46. int next= END_NOT_FOUND;
  47. int flush = !buf_size;
  48. for(i=0; i<buf_size; ){
  49. if(s->frame_size){
  50. int inc= FFMIN(buf_size - i, s->frame_size);
  51. i += inc;
  52. s->frame_size -= inc;
  53. state = 0;
  54. if(!s->frame_size){
  55. next= i;
  56. break;
  57. }
  58. }else{
  59. while(i<buf_size){
  60. int ret, sr, channels, bit_rate, frame_size;
  61. enum AVCodecID codec_id = avctx->codec_id;
  62. state= (state<<8) + buf[i++];
  63. ret = avpriv_mpa_decode_header2(state, &sr, &channels, &frame_size, &bit_rate, &codec_id);
  64. if (ret < 4) {
  65. if (i > 4)
  66. s->header_count = -2;
  67. } else {
  68. int header_threshold = avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec_id;
  69. if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  70. s->header_count= -3;
  71. s->header= state;
  72. s->header_count++;
  73. s->frame_size = ret-4;
  74. if (s->header_count > header_threshold) {
  75. avctx->sample_rate= sr;
  76. avctx->channels = channels;
  77. s1->duration = frame_size;
  78. avctx->codec_id = codec_id;
  79. if (s->no_bitrate || !avctx->bit_rate) {
  80. s->no_bitrate = 1;
  81. avctx->bit_rate += (bit_rate - avctx->bit_rate) / (s->header_count - header_threshold);
  82. }
  83. }
  84. if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  85. s->frame_size = 0;
  86. next = buf_size;
  87. } else if (codec_id == AV_CODEC_ID_MP3ADU) {
  88. avpriv_report_missing_feature(avctx,
  89. "MP3ADU full parser");
  90. return AVERROR_PATCHWELCOME;
  91. }
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. pc->state= state;
  98. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  99. *poutbuf = NULL;
  100. *poutbuf_size = 0;
  101. return buf_size;
  102. }
  103. if (flush && buf_size >= ID3v1_TAG_SIZE && memcmp(buf, "TAG", 3) == 0) {
  104. *poutbuf = NULL;
  105. *poutbuf_size = 0;
  106. return next;
  107. }
  108. *poutbuf = buf;
  109. *poutbuf_size = buf_size;
  110. return next;
  111. }
  112. AVCodecParser ff_mpegaudio_parser = {
  113. .codec_ids = { AV_CODEC_ID_MP1, AV_CODEC_ID_MP2, AV_CODEC_ID_MP3, AV_CODEC_ID_MP3ADU },
  114. .priv_data_size = sizeof(MpegAudioParseContext),
  115. .parser_parse = mpegaudio_parse,
  116. .parser_close = ff_parse_close,
  117. };