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.

150 lines
4.2KB

  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 "mpegaudio.h"
  24. #include "mpegaudiodecheader.h"
  25. typedef struct MpegAudioParseContext {
  26. ParseContext pc;
  27. int frame_size;
  28. uint32_t header;
  29. int header_count;
  30. } MpegAudioParseContext;
  31. #define MPA_HEADER_SIZE 4
  32. /* header + layer + bitrate + freq + lsf/mpeg25 */
  33. #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
  34. #define SAME_HEADER_MASK \
  35. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  36. /* useful helper to get mpeg audio stream infos. Return -1 if error in
  37. header, otherwise the coded frame size in bytes */
  38. int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
  39. {
  40. MPADecodeHeader s1, *s = &s1;
  41. if (ff_mpa_check_header(head) != 0)
  42. return -1;
  43. if (ff_mpegaudio_decode_header(s, head) != 0) {
  44. return -1;
  45. }
  46. switch(s->layer) {
  47. case 1:
  48. avctx->codec_id = CODEC_ID_MP1;
  49. *frame_size = 384;
  50. break;
  51. case 2:
  52. avctx->codec_id = CODEC_ID_MP2;
  53. *frame_size = 1152;
  54. break;
  55. default:
  56. case 3:
  57. avctx->codec_id = CODEC_ID_MP3;
  58. if (s->lsf)
  59. *frame_size = 576;
  60. else
  61. *frame_size = 1152;
  62. break;
  63. }
  64. *sample_rate = s->sample_rate;
  65. *channels = s->nb_channels;
  66. *bit_rate = s->bit_rate;
  67. avctx->sub_id = s->layer;
  68. return s->frame_size;
  69. }
  70. static int mpegaudio_parse(AVCodecParserContext *s1,
  71. AVCodecContext *avctx,
  72. const uint8_t **poutbuf, int *poutbuf_size,
  73. const uint8_t *buf, int buf_size)
  74. {
  75. MpegAudioParseContext *s = s1->priv_data;
  76. ParseContext *pc = &s->pc;
  77. uint32_t state= pc->state;
  78. int i;
  79. int next= END_NOT_FOUND;
  80. for(i=0; i<buf_size; ){
  81. if(s->frame_size){
  82. int inc= FFMIN(buf_size - i, s->frame_size);
  83. i += inc;
  84. s->frame_size -= inc;
  85. if(!s->frame_size){
  86. next= i;
  87. break;
  88. }
  89. }else{
  90. while(i<buf_size){
  91. int ret, sr, channels, bit_rate, frame_size;
  92. state= (state<<8) + buf[i++];
  93. ret = ff_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate);
  94. if (ret < 4) {
  95. s->header_count= -2;
  96. } else {
  97. if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  98. s->header_count= -3;
  99. s->header= state;
  100. s->header_count++;
  101. s->frame_size = ret-4;
  102. if(s->header_count > 1){
  103. avctx->sample_rate= sr;
  104. avctx->channels = channels;
  105. avctx->frame_size = frame_size;
  106. avctx->bit_rate = bit_rate;
  107. }
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. pc->state= state;
  114. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  115. *poutbuf = NULL;
  116. *poutbuf_size = 0;
  117. return buf_size;
  118. }
  119. *poutbuf = buf;
  120. *poutbuf_size = buf_size;
  121. return next;
  122. }
  123. AVCodecParser mpegaudio_parser = {
  124. { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 },
  125. sizeof(MpegAudioParseContext),
  126. NULL,
  127. mpegaudio_parse,
  128. ff_parse_close,
  129. };