latm_parser.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * AAC LATM parser
  23. */
  24. #include <stdint.h>
  25. #include "parser.h"
  26. #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits)
  27. #define LATM_MASK 0xFFE000 // top 11 bits
  28. #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits
  29. typedef struct LATMParseContext{
  30. ParseContext pc;
  31. int count;
  32. } LATMParseContext;
  33. /**
  34. * Find the end of the current frame in the bitstream.
  35. * @return the position of the first byte of the next frame, or -1
  36. */
  37. static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
  38. int buf_size)
  39. {
  40. LATMParseContext *s = s1->priv_data;
  41. ParseContext *pc = &s->pc;
  42. int pic_found, i;
  43. uint32_t state;
  44. pic_found = pc->frame_start_found;
  45. state = pc->state;
  46. if (!pic_found) {
  47. for (i = 0; i < buf_size; i++) {
  48. state = (state<<8) | buf[i];
  49. if ((state & LATM_MASK) == LATM_HEADER) {
  50. i++;
  51. s->count = -i;
  52. pic_found = 1;
  53. break;
  54. }
  55. }
  56. }
  57. if (pic_found) {
  58. /* EOF considered as end of frame */
  59. if (buf_size == 0)
  60. return 0;
  61. if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
  62. pc->frame_start_found = 0;
  63. pc->state = -1;
  64. return (state & LATM_SIZE_MASK) - s->count;
  65. }
  66. }
  67. s->count += buf_size;
  68. pc->frame_start_found = pic_found;
  69. pc->state = state;
  70. return END_NOT_FOUND;
  71. }
  72. static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
  73. const uint8_t **poutbuf, int *poutbuf_size,
  74. const uint8_t *buf, int buf_size)
  75. {
  76. LATMParseContext *s = s1->priv_data;
  77. ParseContext *pc = &s->pc;
  78. int next;
  79. if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  80. next = buf_size;
  81. } else {
  82. next = latm_find_frame_end(s1, buf, buf_size);
  83. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  84. *poutbuf = NULL;
  85. *poutbuf_size = 0;
  86. return buf_size;
  87. }
  88. }
  89. *poutbuf = buf;
  90. *poutbuf_size = buf_size;
  91. return next;
  92. }
  93. AVCodecParser ff_aac_latm_parser = {
  94. .codec_ids = { AV_CODEC_ID_AAC_LATM },
  95. .priv_data_size = sizeof(LATMParseContext),
  96. .parser_parse = latm_parse,
  97. .parser_close = ff_parse_close
  98. };