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.

125 lines
4.0KB

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