FFmpeg
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 "tak.h"
31 
32 typedef struct TAKParseContext {
35  int index;
37 
39  const uint8_t **poutbuf, int *poutbuf_size,
40  const uint8_t *buf, int buf_size)
41 {
42  TAKParseContext *t = s->priv_data;
43  ParseContext *pc = &t->pc;
44  int next = END_NOT_FOUND;
45  GetBitContext gb;
46  int consumed = 0;
47  int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
48  int ret;
49 
50  *poutbuf = buf;
51  *poutbuf_size = buf_size;
52 
53  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
54  TAKStreamInfo ti;
55  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
56  return buf_size;
57  if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
58  s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
59  : t->ti.frame_samples;
60  return buf_size;
61  }
62 
63  while (buf_size || t->index + needed <= pc->index) {
64  if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
65  int tmp_buf_size = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
66  buf_size);
67  const uint8_t *tmp_buf = buf;
68 
69  if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
70  goto fail;
71  consumed += tmp_buf_size;
72  buf += tmp_buf_size;
73  buf_size -= tmp_buf_size;
74  }
75 
76  for (; t->index + needed <= pc->index; t->index++) {
77  if (pc->buffer[ t->index ] == 0xFF &&
78  pc->buffer[ t->index + 1 ] == 0xA0) {
79  TAKStreamInfo ti;
80 
81  if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
82  pc->index - t->index)) < 0)
83  goto fail;
84  if (!ff_tak_decode_frame_header(avctx, &gb,
85  pc->frame_start_found ? &ti : &t->ti, 127) &&
86  !ff_tak_check_crc(pc->buffer + t->index,
87  get_bits_count(&gb) / 8)) {
88  if (!pc->frame_start_found) {
89  pc->frame_start_found = 1;
90  s->duration = t->ti.last_frame_samples ?
92  t->ti.frame_samples;
93  s->key_frame = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
94  } else {
95  pc->frame_start_found = 0;
96  next = t->index - pc->index;
97  t->index = 0;
98  goto found;
99  }
100  }
101  }
102  }
103  }
104 found:
105 
106  if (consumed && !buf_size && next == END_NOT_FOUND ||
107  ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
108  goto fail;
109  }
110 
111  if (next != END_NOT_FOUND) {
112  next += consumed;
113  pc->overread = FFMAX(0, -next);
114  }
115 
116  *poutbuf = buf;
117  *poutbuf_size = buf_size;
118  return next;
119 
120 fail:
121  *poutbuf = NULL;
122  *poutbuf_size = 0;
123  return buf_size + consumed;
124 }
125 
127  .codec_ids = { AV_CODEC_ID_TAK },
128  .priv_data_size = sizeof(TAKParseContext),
129  .parser_parse = tak_parse,
130  .parser_close = ff_parse_close,
131 };
ff_tak_decode_frame_header
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, TAKStreamInfo *ti, int log_level_offset)
Validate and decode a frame header.
Definition: tak.c:143
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:256
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:284
TAKStreamInfo::flags
int flags
Definition: tak.h:128
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
TAK_MAX_FRAME_HEADER_BYTES
#define TAK_MAX_FRAME_HEADER_BYTES
Definition: tak.h:95
TAKStreamInfo::last_frame_samples
int last_frame_samples
Definition: tak.h:136
TAKParseContext::pc
ParseContext pc
Definition: tak_parser.c:33
ParseContext
Definition: parser.h:28
fail
#define fail()
Definition: checkasm.h:134
TAK_FRAME_FLAG_HAS_INFO
#define TAK_FRAME_FLAG_HAS_INFO
Definition: tak.h:60
GetBitContext
Definition: get_bits.h:107
ParseContext::buffer
uint8_t * buffer
Definition: parser.h:29
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:524
ParseContext::index
int index
Definition: parser.h:30
s
#define s(width, name)
Definition: cbs_vp9.c:256
ParseContext::overread
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
NULL
#define NULL
Definition: coverity.c:32
ff_tak_parser
const AVCodecParser ff_tak_parser
Definition: tak_parser.c:126
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
tak.h
TAKParseContext::index
int index
Definition: tak_parser.c:35
index
int index
Definition: gxfenc.c:89
TAKStreamInfo::frame_samples
int frame_samples
Definition: tak.h:135
TAKParseContext
Definition: tak_parser.c:32
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2972
ff_tak_check_crc
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:79
TAKParseContext::ti
TAKStreamInfo ti
Definition: tak_parser.c:34
ff_combine_frame
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:199
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2846
tak_parse
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:38
needed
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is needed
Definition: filter_design.txt:212
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
parser.h
AVCodecParserContext
Definition: avcodec.h:2812
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:426
TAKStreamInfo
Definition: tak.h:127
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2971
AV_CODEC_ID_TAK
@ AV_CODEC_ID_TAK
Definition: codec_id.h:500