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.

110 lines
3.3KB

  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. 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. static int mpegaudio_parse(AVCodecParserContext *s1,
  36. AVCodecContext *avctx,
  37. const uint8_t **poutbuf, int *poutbuf_size,
  38. const uint8_t *buf, int buf_size)
  39. {
  40. MpegAudioParseContext *s = s1->priv_data;
  41. ParseContext *pc = &s->pc;
  42. uint32_t state= pc->state;
  43. int i;
  44. int next= END_NOT_FOUND;
  45. for(i=0; i<buf_size; ){
  46. if(s->frame_size){
  47. int inc= FFMIN(buf_size - i, s->frame_size);
  48. i += inc;
  49. s->frame_size -= inc;
  50. if(!s->frame_size){
  51. next= i;
  52. break;
  53. }
  54. }else{
  55. while(i<buf_size){
  56. int ret, sr, channels, bit_rate, frame_size;
  57. state= (state<<8) + buf[i++];
  58. ret = avpriv_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate);
  59. if (ret < 4) {
  60. if (i > 4)
  61. s->header_count = -2;
  62. } else {
  63. if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  64. s->header_count= -3;
  65. s->header= state;
  66. s->header_count++;
  67. s->frame_size = ret-4;
  68. if (s->header_count > 1) {
  69. avctx->sample_rate= sr;
  70. avctx->channels = channels;
  71. s1->duration = frame_size;
  72. avctx->bit_rate = bit_rate;
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. pc->state= state;
  80. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  81. *poutbuf = NULL;
  82. *poutbuf_size = 0;
  83. return buf_size;
  84. }
  85. *poutbuf = buf;
  86. *poutbuf_size = buf_size;
  87. return next;
  88. }
  89. AVCodecParser ff_mpegaudio_parser = {
  90. .codec_ids = { AV_CODEC_ID_MP1, AV_CODEC_ID_MP2, AV_CODEC_ID_MP3 },
  91. .priv_data_size = sizeof(MpegAudioParseContext),
  92. .parser_parse = mpegaudio_parse,
  93. .parser_close = ff_parse_close,
  94. };