]> git.sesse.net Git - ffmpeg/blob - libavcodec/tak_parser.c
huffyuv: make use of av_fast_padded_malloc()
[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 "parser.h"
28 #include "tak.h"
29
30 typedef struct TAKParseContext {
31     ParseContext  pc;
32     TAKStreamInfo ti;
33     int index;
34 } TAKParseContext;
35
36 static av_cold int tak_init(AVCodecParserContext *s)
37 {
38     ff_tak_init_crc();
39     return 0;
40 }
41
42 static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
43                      const uint8_t **poutbuf, int *poutbuf_size,
44                      const uint8_t *buf, int buf_size)
45 {
46     TAKParseContext *t = s->priv_data;
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
53     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
54         TAKStreamInfo ti;
55         init_get_bits(&gb, buf, buf_size);
56         if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
57             s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples :
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, buf_size);
67             const uint8_t *tmp_buf = buf;
68
69             if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
70                 return AVERROR(ENOMEM);
71             consumed += tmp_buf_size;
72             buf      += tmp_buf_size;
73             buf_size -= tmp_buf_size;
74         }
75
76         for (; t->index + needed <= pc->index; t->index++) {
77             if (pc->buffer[ t->index     ] == 0xFF &&
78                 pc->buffer[ t->index + 1 ] == 0xA0) {
79                 TAKStreamInfo ti;
80
81                 init_get_bits(&gb, pc->buffer + t->index,
82                               8 * (pc->index - t->index));
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;
89                         s->duration = t->ti.last_frame_samples ?
90                                       t->ti.last_frame_samples :
91                                       t->ti.frame_samples;
92                     } else {
93                         pc->frame_start_found = 0;
94                         next = t->index - pc->index;
95                         t->index = 0;
96                         goto found;
97                     }
98                 }
99             }
100         }
101     }
102 found:
103
104     if (consumed && !buf_size && next == END_NOT_FOUND ||
105         ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
106         *poutbuf      = NULL;
107         *poutbuf_size = 0;
108         return buf_size + consumed;
109     }
110
111     if (next != END_NOT_FOUND) {
112         next += consumed;
113         pc->overread = FFMAX(0, -next);
114     }
115
116     *poutbuf      = buf;
117     *poutbuf_size = buf_size;
118     return next;
119 }
120
121 AVCodecParser ff_tak_parser = {
122     .codec_ids      = { AV_CODEC_ID_TAK },
123     .priv_data_size = sizeof(TAKParseContext),
124     .parser_init    = tak_init,
125     .parser_parse   = tak_parse,
126     .parser_close   = ff_parse_close,
127 };