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 #include "tak.h"
28 #include "parser.h"
29 
30 typedef struct TAKParseContext {
33  int index;
35 
37 {
39  return 0;
40 }
41 
43  const uint8_t **poutbuf, int *poutbuf_size,
44  const uint8_t *buf, int buf_size)
45 {
47  ParseContext *pc = &t->pc;
48  int next = END_NOT_FOUND;
49  GetBitContext gb;
50  int consumed = 0;
51  int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
52 
55  init_get_bits(&gb, buf, buf_size);
56  if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
58  : t->ti.frame_samples;
59  *poutbuf = buf;
60  *poutbuf_size = buf_size;
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(2 * 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  return AVERROR(ENOMEM);
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  init_get_bits(&gb, pc->buffer + t->index,
83  8 * (pc->index - t->index));
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;
92  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  *poutbuf = NULL;
108  *poutbuf_size = 0;
109  return buf_size + consumed;
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 }
121 
123  .codec_ids = { AV_CODEC_ID_TAK },
124  .priv_data_size = sizeof(TAKParseContext),
125  .parser_init = tak_init,
126  .parser_parse = tak_parse,
127  .parser_close = ff_parse_close,
128 };
const char * s
Definition: avisynth_c.h:668
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
AVCodecParser ff_tak_parser
Definition: tak_parser.c:122
int duration
Duration of the current frame.
int frame_start_found
Definition: parser.h:34
TAKStreamInfo ti
Definition: tak_parser.c:32
uint8_t
#define av_cold
Definition: attributes.h:78
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:42
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:92
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
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:214
#define PARSER_FLAG_COMPLETE_FRAMES
av_cold void ff_tak_init_crc(void)
Definition: tak.c:84
int last_frame_samples
Definition: tak.h:138
#define FFMAX(a, b)
Definition: common.h:56
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:279
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:58
static av_cold int tak_init(AVCodecParserContext *s)
Definition: tak_parser.c:36
t
Definition: genspecsines3.m:6
NULL
Definition: eval.c:55
uint8_t * buffer
Definition: parser.h:29
ParseContext pc
Definition: tak_parser.c:31
main external API structure.
void * buf
Definition: avisynth_c.h:594
int frame_samples
Definition: tak.h:137
TAK (Tom&#39;s lossless Audio Kompressor) decoder/demuxer common functions.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
#define END_NOT_FOUND
Definition: parser.h:40
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
struct TAKParseContext TAKParseContext
int index
Definition: parser.h:30
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted then it is pulled from the input slice through the input converter and horizontal scaler The result is also stored in the ring buffer to serve future vertical scaler requests When no more output can be generated because lines from a future slice would be needed
Definition: swscale.txt:33
#define TAK_MAX_FRAME_HEADER_BYTES
Definition: tak.h:97