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.

120 lines
3.2KB

  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <sys/types.h>
  29. #include "parser.h"
  30. #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits)
  31. #define LATM_MASK 0xFFE000 // top 11 bits
  32. #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits
  33. typedef struct LATMParseContext{
  34. ParseContext pc;
  35. int count;
  36. } LATMParseContext;
  37. /**
  38. * finds the end of the current frame in the bitstream.
  39. * @return the position of the first byte of the next frame, or -1
  40. */
  41. static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
  42. int buf_size)
  43. {
  44. LATMParseContext *s = s1->priv_data;
  45. ParseContext *pc = &s->pc;
  46. int pic_found, i;
  47. uint32_t state;
  48. pic_found = pc->frame_start_found;
  49. state = pc->state;
  50. i = 0;
  51. if (!pic_found) {
  52. for (i = 0; i < buf_size; i++) {
  53. state = (state<<8) | buf[i];
  54. if ((state & LATM_MASK) == LATM_HEADER) {
  55. i++;
  56. s->count = -i;
  57. pic_found = 1;
  58. break;
  59. }
  60. }
  61. }
  62. if (pic_found) {
  63. /* EOF considered as end of frame */
  64. if (buf_size == 0)
  65. return 0;
  66. if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
  67. pc->frame_start_found = 0;
  68. pc->state = -1;
  69. return (state & LATM_SIZE_MASK) - s->count;
  70. }
  71. }
  72. s->count += buf_size;
  73. pc->frame_start_found = pic_found;
  74. pc->state = state;
  75. return END_NOT_FOUND;
  76. }
  77. static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
  78. const uint8_t **poutbuf, int *poutbuf_size,
  79. const uint8_t *buf, int buf_size)
  80. {
  81. LATMParseContext *s = s1->priv_data;
  82. ParseContext *pc = &s->pc;
  83. int next;
  84. if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  85. next = buf_size;
  86. } else {
  87. next = latm_find_frame_end(s1, buf, buf_size);
  88. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  89. *poutbuf = NULL;
  90. *poutbuf_size = 0;
  91. return buf_size;
  92. }
  93. }
  94. *poutbuf = buf;
  95. *poutbuf_size = buf_size;
  96. return next;
  97. }
  98. AVCodecParser aac_latm_parser = {
  99. { CODEC_ID_AAC_LATM },
  100. sizeof(LATMParseContext),
  101. NULL,
  102. latm_parse,
  103. ff_parse_close
  104. };