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.

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