FFmpeg
Loading...
Searching...
No Matches
tak_parser.c
Go to the documentation of this file.
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/**
23 * @file
24 * TAK parser
25 **/
26
27#define CACHED_BITSTREAM_READER !ARCH_X86_32
28#define BITSTREAM_READER_LE
29#include "parser.h"
30#include "parser_internal.h"
31#include "tak.h"
32
38
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;
47 int consumed = 0;
48 int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
49 int ret;
50
51 *poutbuf = buf;
52 *poutbuf_size = buf_size;
53
54 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
56 if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
57 return buf_size;
58 if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
59 s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
60 : t->ti.frame_samples;
61 return buf_size;
62 }
63
64 while (buf_size || t->index + needed <= pc->index) {
65 if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
66 int tmp_buf_size = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
67 buf_size);
68 const uint8_t *tmp_buf = buf;
69
70 if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
71 goto fail;
72 consumed += tmp_buf_size;
73 buf += tmp_buf_size;
74 buf_size -= tmp_buf_size;
75 }
76
77 for (; t->index + needed <= pc->index; t->index++) {
78 if (pc->buffer[ t->index ] == 0xFF &&
79 pc->buffer[ t->index + 1 ] == 0xA0) {
81
82 if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
83 pc->index - t->index)) < 0)
84 goto fail;
85 if (!ff_tak_decode_frame_header(avctx, &gb,
86 pc->frame_start_found ? &ti : &t->ti, 127) &&
88 get_bits_count(&gb) / 8)) {
89 if (!pc->frame_start_found) {
90 pc->frame_start_found = 1;
91 s->duration = t->ti.last_frame_samples ?
94 s->key_frame = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
95 } else {
96 pc->frame_start_found = 0;
97 next = t->index - pc->index;
98 t->index = 0;
99 goto found;
100 }
101 }
102 }
103 }
104 }
105found:
106
107 if (consumed && !buf_size && next == END_NOT_FOUND ||
108 ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
109 goto fail;
110 }
111
112 if (next != END_NOT_FOUND) {
113 next += consumed;
114 pc->overread = FFMAX(0, -next);
115 }
116
117 *poutbuf = buf;
118 *poutbuf_size = buf_size;
119 return next;
120
121fail:
122 *poutbuf = NULL;
123 *poutbuf_size = 0;
124 return buf_size + consumed;
125}
126
129 .priv_data_size = sizeof(TAKParseContext),
130 .parse = tak_parse,
132};
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
#define fail
Definition test.h:479
@ AV_CODEC_ID_TAK
Definition codec_id.h:515
int index
Definition gxfenc.c:90
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
const FFCodecParser ff_tak_parser
Definition tak_parser.c:127
main external API structure.
Definition avcodec.h:443
int index
Definition parser.h:30
int frame_start_found
Definition parser.h:34
int overread
the number of bytes which where irreversibly read from the next frame
Definition parser.h:35
uint8_t * buffer
Definition parser.h:29
TAKStreamInfo ti
Definition tak_parser.c:35
ParseContext pc
Definition tak_parser.c:34
int last_frame_samples
Definition tak.h:135
int frame_samples
Definition tak.h:134
int flags
Definition tak.h:127
int ff_tak_decode_frame_header(void *logctx, GetBitContext *gb, TAKStreamInfo *ti, int log_level_offset)
Validate and decode a frame header.
Definition tak.c:147
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition tak.c:79
TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions.
#define TAK_MAX_FRAME_HEADER_BYTES
Definition tak.h:94
#define TAK_FRAME_FLAG_HAS_INFO
Definition tak.h:59
static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition tak_parser.c:39