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.

131 lines
4.4KB

  1. /*
  2. * TAK parser
  3. * Copyright (c) 2012 Michael Niedermayer
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "bitstream.h"
  27. #include "parser.h"
  28. #include "tak.h"
  29. typedef struct TAKParseContext {
  30. ParseContext pc;
  31. TAKStreamInfo ti;
  32. int index;
  33. } TAKParseContext;
  34. static av_cold int tak_init(AVCodecParserContext *s)
  35. {
  36. ff_tak_init_crc();
  37. return 0;
  38. }
  39. static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  40. const uint8_t **poutbuf, int *poutbuf_size,
  41. const uint8_t *buf, int buf_size)
  42. {
  43. TAKParseContext *t = s->priv_data;
  44. ParseContext *pc = &t->pc;
  45. int next = END_NOT_FOUND;
  46. BitstreamContext bc;
  47. int consumed = 0;
  48. int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
  49. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  50. TAKStreamInfo ti;
  51. bitstream_init(&bc, buf, buf_size);
  52. if (!ff_tak_decode_frame_header(avctx, &bc, &ti, 127))
  53. s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
  54. : t->ti.frame_samples;
  55. *poutbuf = buf;
  56. *poutbuf_size = buf_size;
  57. return buf_size;
  58. }
  59. while (buf_size || t->index + needed <= pc->index) {
  60. if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
  61. int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
  62. buf_size);
  63. const uint8_t *tmp_buf = buf;
  64. ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);
  65. consumed += tmp_buf_size;
  66. buf += tmp_buf_size;
  67. buf_size -= tmp_buf_size;
  68. }
  69. for (; t->index + needed <= pc->index; t->index++)
  70. if (pc->buffer[t->index] == 0xFF &&
  71. pc->buffer[t->index + 1] == 0xA0) {
  72. TAKStreamInfo ti;
  73. bitstream_init8(&bc, pc->buffer + t->index,
  74. pc->index - t->index);
  75. if (!ff_tak_decode_frame_header(avctx, &bc,
  76. pc->frame_start_found ? &ti
  77. : &t->ti,
  78. 127) &&
  79. !ff_tak_check_crc(pc->buffer + t->index,
  80. bitstream_tell(&bc) / 8)) {
  81. if (!pc->frame_start_found) {
  82. pc->frame_start_found = 1;
  83. s->duration = t->ti.last_frame_samples ?
  84. t->ti.last_frame_samples :
  85. t->ti.frame_samples;
  86. } else {
  87. pc->frame_start_found = 0;
  88. next = t->index - pc->index;
  89. t->index = 0;
  90. goto found;
  91. }
  92. }
  93. }
  94. }
  95. found:
  96. if (consumed && !buf_size && next == END_NOT_FOUND ||
  97. ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  98. *poutbuf = NULL;
  99. *poutbuf_size = 0;
  100. return buf_size + consumed;
  101. }
  102. if (next != END_NOT_FOUND) {
  103. next += consumed;
  104. pc->overread = FFMAX(0, -next);
  105. }
  106. *poutbuf = buf;
  107. *poutbuf_size = buf_size;
  108. return next;
  109. }
  110. AVCodecParser ff_tak_parser = {
  111. .codec_ids = { AV_CODEC_ID_TAK },
  112. .priv_data_size = sizeof(TAKParseContext),
  113. .parser_init = tak_init,
  114. .parser_parse = tak_parse,
  115. .parser_close = ff_parse_close,
  116. };