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.

127 lines
4.3KB

  1. /*
  2. * TAK parser
  3. * Copyright (c) 2012 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * TAK parser
  24. **/
  25. #define BITSTREAM_READER_LE
  26. #include "parser.h"
  27. #include "tak.h"
  28. typedef struct TAKParseContext {
  29. ParseContext pc;
  30. TAKStreamInfo ti;
  31. int index;
  32. } TAKParseContext;
  33. static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  34. const uint8_t **poutbuf, int *poutbuf_size,
  35. const uint8_t *buf, int buf_size)
  36. {
  37. TAKParseContext *t = s->priv_data;
  38. ParseContext *pc = &t->pc;
  39. int next = END_NOT_FOUND;
  40. GetBitContext gb;
  41. int consumed = 0;
  42. int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
  43. int ret;
  44. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  45. TAKStreamInfo ti;
  46. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  47. return ret;
  48. if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
  49. s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
  50. : t->ti.frame_samples;
  51. *poutbuf = buf;
  52. *poutbuf_size = buf_size;
  53. return buf_size;
  54. }
  55. while (buf_size || t->index + needed <= pc->index) {
  56. if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
  57. int tmp_buf_size = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
  58. buf_size);
  59. const uint8_t *tmp_buf = buf;
  60. if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
  61. return AVERROR(ENOMEM);
  62. consumed += tmp_buf_size;
  63. buf += tmp_buf_size;
  64. buf_size -= tmp_buf_size;
  65. }
  66. for (; t->index + needed <= pc->index; t->index++) {
  67. if (pc->buffer[ t->index ] == 0xFF &&
  68. pc->buffer[ t->index + 1 ] == 0xA0) {
  69. TAKStreamInfo ti;
  70. if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
  71. pc->index - t->index)) < 0)
  72. return ret;
  73. if (!ff_tak_decode_frame_header(avctx, &gb,
  74. pc->frame_start_found ? &ti : &t->ti, 127) &&
  75. !ff_tak_check_crc(pc->buffer + t->index,
  76. get_bits_count(&gb) / 8)) {
  77. if (!pc->frame_start_found) {
  78. pc->frame_start_found = 1;
  79. s->duration = t->ti.last_frame_samples ?
  80. t->ti.last_frame_samples :
  81. t->ti.frame_samples;
  82. s->key_frame = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
  83. } else {
  84. pc->frame_start_found = 0;
  85. next = t->index - pc->index;
  86. t->index = 0;
  87. goto found;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. found:
  94. if (consumed && !buf_size && next == END_NOT_FOUND ||
  95. ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  96. *poutbuf = NULL;
  97. *poutbuf_size = 0;
  98. return buf_size + consumed;
  99. }
  100. if (next != END_NOT_FOUND) {
  101. next += consumed;
  102. pc->overread = FFMAX(0, -next);
  103. }
  104. *poutbuf = buf;
  105. *poutbuf_size = buf_size;
  106. return next;
  107. }
  108. AVCodecParser ff_tak_parser = {
  109. .codec_ids = { AV_CODEC_ID_TAK },
  110. .priv_data_size = sizeof(TAKParseContext),
  111. .parser_parse = tak_parse,
  112. .parser_close = ff_parse_close,
  113. };