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 BITSTREAM_READER_LE
28 #include "parser.h"
29 #include "tak.h"
30 
31 typedef struct TAKParseContext {
34  int index;
36 
38  const uint8_t **poutbuf, int *poutbuf_size,
39  const uint8_t *buf, int buf_size)
40 {
41  TAKParseContext *t = s->priv_data;
42  ParseContext *pc = &t->pc;
43  int next = END_NOT_FOUND;
44  GetBitContext gb;
45  int consumed = 0;
46  int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
47  int ret;
48 
49  *poutbuf = buf;
50  *poutbuf_size = buf_size;
51 
54  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
55  return buf_size;
56  if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
58  : t->ti.frame_samples;
59  return buf_size;
60  }
61 
62  while (buf_size || t->index + needed <= pc->index) {
63  if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
64  int tmp_buf_size = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
65  buf_size);
66  const uint8_t *tmp_buf = buf;
67 
68  if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
69  goto fail;
70  consumed += tmp_buf_size;
71  buf += tmp_buf_size;
72  buf_size -= tmp_buf_size;
73  }
74 
75  for (; t->index + needed <= pc->index; t->index++) {
76  if (pc->buffer[ t->index ] == 0xFF &&
77  pc->buffer[ t->index + 1 ] == 0xA0) {
79 
80  if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
81  pc->index - t->index)) < 0)
82  goto fail;
83  if (!ff_tak_decode_frame_header(avctx, &gb,
84  pc->frame_start_found ? &ti : &t->ti, 127) &&
85  !ff_tak_check_crc(pc->buffer + t->index,
86  get_bits_count(&gb) / 8)) {
87  if (!pc->frame_start_found) {
88  pc->frame_start_found = 1;
91  t->ti.frame_samples;
93  } else {
94  pc->frame_start_found = 0;
95  next = t->index - pc->index;
96  t->index = 0;
97  goto found;
98  }
99  }
100  }
101  }
102  }
103 found:
104 
105  if (consumed && !buf_size && next == END_NOT_FOUND ||
106  ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
107  goto fail;
108  }
109 
110  if (next != END_NOT_FOUND) {
111  next += consumed;
112  pc->overread = FFMAX(0, -next);
113  }
114 
115  *poutbuf = buf;
116  *poutbuf_size = buf_size;
117  return next;
118 
119 fail:
120  *poutbuf = NULL;
121  *poutbuf_size = 0;
122  return buf_size + consumed;
123 }
124 
126  .codec_ids = { AV_CODEC_ID_TAK },
127  .priv_data_size = sizeof(TAKParseContext),
128  .parser_parse = tak_parse,
129  .parser_close = ff_parse_close,
130 };
#define NULL
Definition: coverity.c:32
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:141
int codec_ids[5]
Definition: avcodec.h:5276
AVCodecParser ff_tak_parser
Definition: tak_parser.c:125
int duration
Duration of the current frame.
Definition: avcodec.h:5230
int frame_start_found
Definition: parser.h:34
TAKStreamInfo ti
Definition: tak_parser.c:33
uint8_t
int flags
Definition: tak.h:129
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:37
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:77
#define TAK_FRAME_FLAG_HAS_INFO
Definition: tak.h:61
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
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:234
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
int last_frame_samples
Definition: tak.h:137
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:120
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:315
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
#define FFMIN(a, b)
Definition: common.h:96
#define s(width, name)
Definition: cbs_vp9.c:257
uint8_t * buffer
Definition: parser.h:29
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
ParseContext pc
Definition: tak_parser.c:32
main external API structure.
Definition: avcodec.h:1565
void * buf
Definition: avisynth_c.h:766
int frame_samples
Definition: tak.h:136
TAK (Tom&#39;s lossless Audio Kompressor) decoder/demuxer common functions.
#define END_NOT_FOUND
Definition: parser.h:40
int index
Definition: parser.h:30
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5142
#define TAK_MAX_FRAME_HEADER_BYTES
Definition: tak.h:96
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:5157