]> git.sesse.net Git - ffmpeg/blob - libavcodec/tak_parser.c
avcodec/vc1: Make init_block_index() inline
[ffmpeg] / libavcodec / tak_parser.c
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 {
31     ParseContext  pc;
32     TAKStreamInfo ti;
33     int           index;
34 } TAKParseContext;
35
36 static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
37                      const uint8_t **poutbuf, int *poutbuf_size,
38                      const uint8_t *buf, int buf_size)
39 {
40     TAKParseContext *t = s->priv_data;
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     int ret;
47
48     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
49         TAKStreamInfo ti;
50         if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
51             return ret;
52         if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
53             s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
54                                                    : t->ti.frame_samples;
55         *poutbuf      = buf;
56         *poutbuf_size = buf_size;
57         return buf_size;
58     }
59
60     while (buf_size || t->index + needed <= pc->index) {
61         if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
62             int tmp_buf_size       = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
63                                            buf_size);
64             const uint8_t *tmp_buf = buf;
65
66             if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
67                 return AVERROR(ENOMEM);
68             consumed += tmp_buf_size;
69             buf      += tmp_buf_size;
70             buf_size -= tmp_buf_size;
71         }
72
73         for (; t->index + needed <= pc->index; t->index++) {
74             if (pc->buffer[ t->index     ] == 0xFF &&
75                 pc->buffer[ t->index + 1 ] == 0xA0) {
76                 TAKStreamInfo ti;
77
78                 if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
79                                           pc->index - t->index)) < 0)
80                     return ret;
81                 if (!ff_tak_decode_frame_header(avctx, &gb,
82                         pc->frame_start_found ? &ti : &t->ti, 127) &&
83                     !ff_tak_check_crc(pc->buffer + t->index,
84                                       get_bits_count(&gb) / 8)) {
85                     if (!pc->frame_start_found) {
86                         pc->frame_start_found = 1;
87                         s->duration           = t->ti.last_frame_samples ?
88                                                 t->ti.last_frame_samples :
89                                                 t->ti.frame_samples;
90                         s->key_frame          = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
91                     } else {
92                         pc->frame_start_found = 0;
93                         next                  = t->index - pc->index;
94                         t->index              = 0;
95                         goto found;
96                     }
97                 }
98             }
99         }
100     }
101 found:
102
103     if (consumed && !buf_size && next == END_NOT_FOUND ||
104         ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
105         *poutbuf      = NULL;
106         *poutbuf_size = 0;
107         return buf_size + consumed;
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
120 AVCodecParser ff_tak_parser = {
121     .codec_ids      = { AV_CODEC_ID_TAK },
122     .priv_data_size = sizeof(TAKParseContext),
123     .parser_parse   = tak_parse,
124     .parser_close   = ff_parse_close,
125 };