FFmpeg  2.1.1
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  const uint8_t **poutbuf, int *poutbuf_size,
38  const uint8_t *buf, int buf_size)
39 {
41  ParseContext *pc = &t->pc;
42  int next = END_NOT_FOUND;
43  GetBitContext gb;
44  int consumed = 0;
45  int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
46 
48  TAKStreamInfo ti;
49  init_get_bits(&gb, buf, buf_size);
50  if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
52  : t->ti.frame_samples;
53  *poutbuf = buf;
54  *poutbuf_size = buf_size;
55  return buf_size;
56  }
57 
58  while (buf_size || t->index + needed <= pc->index) {
59  if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
60  int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
61  buf_size);
62  const uint8_t *tmp_buf = buf;
63 
64  if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
65  return AVERROR(ENOMEM);
66  consumed += tmp_buf_size;
67  buf += tmp_buf_size;
68  buf_size -= tmp_buf_size;
69  }
70 
71  for (; t->index + needed <= pc->index; t->index++) {
72  if (pc->buffer[ t->index ] == 0xFF &&
73  pc->buffer[ t->index + 1 ] == 0xA0) {
74  TAKStreamInfo ti;
75 
76  init_get_bits(&gb, pc->buffer + t->index,
77  8 * (pc->index - t->index));
78  if (!ff_tak_decode_frame_header(avctx, &gb,
79  pc->frame_start_found ? &ti : &t->ti, 127) &&
80  !ff_tak_check_crc(pc->buffer + t->index,
81  get_bits_count(&gb) / 8)) {
82  if (!pc->frame_start_found) {
83  pc->frame_start_found = 1;
86  t->ti.frame_samples;
88  } else {
89  pc->frame_start_found = 0;
90  next = t->index - pc->index;
91  t->index = 0;
92  goto found;
93  }
94  }
95  }
96  }
97  }
98 found:
99 
100  if (consumed && !buf_size && next == END_NOT_FOUND ||
101  ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
102  *poutbuf = NULL;
103  *poutbuf_size = 0;
104  return buf_size + consumed;
105  }
106 
107  if (next != END_NOT_FOUND) {
108  next += consumed;
109  pc->overread = FFMAX(0, -next);
110  }
111 
112  *poutbuf = buf;
113  *poutbuf_size = buf_size;
114  return next;
115 }
116 
118  .codec_ids = { AV_CODEC_ID_TAK },
119  .priv_data_size = sizeof(TAKParseContext),
120  .parser_parse = tak_parse,
121  .parser_close = ff_parse_close,
122 };
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:126
AVCodecParser ff_tak_parser
Definition: tak_parser.c:117
int duration
Duration of the current frame.
Definition: avcodec.h:4060
int frame_start_found
Definition: parser.h:34
TAKStreamInfo ti
Definition: tak_parser.c:32
uint8_t
int flags
Definition: tak.h:130
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:36
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:75
#define TAK_FRAME_FLAG_HAS_INFO
Definition: tak.h:62
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:207
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:216
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3961
int last_frame_samples
Definition: tak.h:138
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:285
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
#define FFMIN(a, b)
Definition: avcodec.h:925
uint8_t * buffer
Definition: parser.h:29
#define FFMAX(a, b)
Definition: avcodec.h:923
ParseContext pc
Definition: tak_parser.c:31
int codec_ids[5]
Definition: avcodec.h:4084
main external API structure.
Definition: avcodec.h:1146
void * buf
Definition: avisynth_c.h:594
int index
Definition: gxfenc.c:89
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:405
#define END_NOT_FOUND
Definition: parser.h:40
static float t
Definition: muxing.c:123
int index
Definition: parser.h:30
#define AVERROR(e)
#define TAK_MAX_FRAME_HEADER_BYTES
Definition: tak.h:97
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3976