Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #define BITSTREAM_READER_LE
28 #include "parser.h"
29 #include "tak.h"
30 
31 typedef struct TAKParseContext {
34  int index;
36 
38 {
40  return 0;
41 }
42 
44  const uint8_t **poutbuf, int *poutbuf_size,
45  const uint8_t *buf, int buf_size)
46 {
47  TAKParseContext *t = s->priv_data;
48  ParseContext *pc = &t->pc;
49  int next = END_NOT_FOUND;
50  GetBitContext gb;
51  int consumed = 0;
52  int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
53 
56  init_get_bits(&gb, buf, buf_size);
57  if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
59  : t->ti.frame_samples;
60  *poutbuf = buf;
61  *poutbuf_size = buf_size;
62  return buf_size;
63  }
64 
65  while (buf_size || t->index + needed <= pc->index) {
66  if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
67  int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
68  buf_size);
69  const uint8_t *tmp_buf = buf;
70 
71  ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);
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
86  : &t->ti,
87  127) &&
88  !ff_tak_check_crc(pc->buffer + t->index,
89  get_bits_count(&gb) / 8)) {
90  if (!pc->frame_start_found) {
91  pc->frame_start_found = 1;
94  t->ti.frame_samples;
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 
105 found:
106  if (consumed && !buf_size && next == END_NOT_FOUND ||
107  ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
108  *poutbuf = NULL;
109  *poutbuf_size = 0;
110  return buf_size + consumed;
111  }
112 
113  if (next != END_NOT_FOUND) {
114  next += consumed;
115  pc->overread = FFMAX(0, -next);
116  }
117 
118  *poutbuf = buf;
119  *poutbuf_size = buf_size;
120  return next;
121 }
122 
124  .codec_ids = { AV_CODEC_ID_TAK },
125  .priv_data_size = sizeof(TAKParseContext),
126  .parser_init = tak_init,
127  .parser_parse = tak_parse,
128  .parser_close = ff_parse_close,
129 };
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:123
int codec_ids[5]
Definition: avcodec.h:4529
AVCodecParser ff_tak_parser
Definition: tak_parser.c:123
int duration
Duration of the current frame.
Definition: avcodec.h:4483
int frame_start_found
Definition: parser.h:34
TAKStreamInfo ti
Definition: tak_parser.c:33
uint8_t
#define av_cold
Definition: attributes.h:66
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:43
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:72
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
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:227
av_cold void ff_tak_init_crc(void)
Definition: tak.c:64
int last_frame_samples
Definition: tak.h:137
#define FFMAX(a, b)
Definition: common.h:64
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:297
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:66
static av_cold int tak_init(AVCodecParserContext *s)
Definition: tak_parser.c:37
NULL
Definition: eval.c:55
uint8_t * buffer
Definition: parser.h:29
ParseContext pc
Definition: tak_parser.c:32
main external API structure.
Definition: avcodec.h:1409
int frame_samples
Definition: tak.h:136
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:362
#define END_NOT_FOUND
Definition: parser.h:40
int index
Definition: parser.h:30
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4396
#define TAK_MAX_FRAME_HEADER_BYTES
Definition: tak.h:96