3 * Copyright (c) 2012 Michael Niedermayer
5 * This file is part of FFmpeg.
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.
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.
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
27 #define BITSTREAM_READER_LE
31 typedef struct TAKParseContext {
37 static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
38 const uint8_t **poutbuf, int *poutbuf_size,
39 const uint8_t *buf, int buf_size)
41 TAKParseContext *t = s->priv_data;
42 ParseContext *pc = &t->pc;
43 int next = END_NOT_FOUND;
46 int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
49 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
51 if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
53 if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
54 s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
55 : t->ti.frame_samples;
57 *poutbuf_size = buf_size;
61 while (buf_size || t->index + needed <= pc->index) {
62 if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
63 int tmp_buf_size = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
65 const uint8_t *tmp_buf = buf;
67 if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
68 return AVERROR(ENOMEM);
69 consumed += tmp_buf_size;
71 buf_size -= tmp_buf_size;
74 for (; t->index + needed <= pc->index; t->index++) {
75 if (pc->buffer[ t->index ] == 0xFF &&
76 pc->buffer[ t->index + 1 ] == 0xA0) {
79 if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
80 pc->index - t->index)) < 0)
82 if (!ff_tak_decode_frame_header(avctx, &gb,
83 pc->frame_start_found ? &ti : &t->ti, 127) &&
84 !ff_tak_check_crc(pc->buffer + t->index,
85 get_bits_count(&gb) / 8)) {
86 if (!pc->frame_start_found) {
87 pc->frame_start_found = 1;
88 s->duration = t->ti.last_frame_samples ?
89 t->ti.last_frame_samples :
91 s->key_frame = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
93 pc->frame_start_found = 0;
94 next = t->index - pc->index;
104 if (consumed && !buf_size && next == END_NOT_FOUND ||
105 ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
108 return buf_size + consumed;
111 if (next != END_NOT_FOUND) {
113 pc->overread = FFMAX(0, -next);
117 *poutbuf_size = buf_size;
121 AVCodecParser ff_tak_parser = {
122 .codec_ids = { AV_CODEC_ID_TAK },
123 .priv_data_size = sizeof(TAKParseContext),
124 .parser_parse = tak_parse,
125 .parser_close = ff_parse_close,