]> git.sesse.net Git - ffmpeg/blob - libavcodec/dfa.c
hevc: remove HEVCContext usage from hevc_ps
[ffmpeg] / libavcodec / dfa.c
1 /*
2  * Chronomaster DFA Video Decoder
3  * Copyright (c) 2011 Konstantin Shishkov
4  * based on work by Vladimir "VAG" Gneushev
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <inttypes.h>
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "internal.h"
28
29 #include "libavutil/imgutils.h"
30 #include "libavutil/mem.h"
31
32 typedef struct DfaContext {
33     uint32_t pal[256];
34     uint8_t *frame_buf;
35 } DfaContext;
36
37 static av_cold int dfa_decode_init(AVCodecContext *avctx)
38 {
39     DfaContext *s = avctx->priv_data;
40     int ret;
41
42     avctx->pix_fmt = AV_PIX_FMT_PAL8;
43
44     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
45         return ret;
46
47     s->frame_buf = av_mallocz(avctx->width * avctx->height);
48     if (!s->frame_buf)
49         return AVERROR(ENOMEM);
50
51     return 0;
52 }
53
54 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
55 {
56     const int size = width * height;
57
58     if (bytestream2_get_buffer(gb, frame, size) != size)
59         return AVERROR_INVALIDDATA;
60     return 0;
61 }
62
63 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
64 {
65     const uint8_t *frame_start = frame;
66     const uint8_t *frame_end   = frame + width * height;
67     int mask = 0x10000, bitbuf = 0;
68     int v, count, segments;
69     unsigned offset;
70
71     segments = bytestream2_get_le32(gb);
72     offset   = bytestream2_get_le32(gb);
73     if (frame_end - frame <= offset)
74         return AVERROR_INVALIDDATA;
75     frame += offset;
76     while (segments--) {
77         if (bytestream2_get_bytes_left(gb) < 2)
78             return AVERROR_INVALIDDATA;
79         if (mask == 0x10000) {
80             bitbuf = bytestream2_get_le16u(gb);
81             mask = 1;
82         }
83         if (frame_end - frame < 2)
84             return AVERROR_INVALIDDATA;
85         if (bitbuf & mask) {
86             v = bytestream2_get_le16(gb);
87             offset = (v & 0x1FFF) << 1;
88             count = ((v >> 13) + 2) << 1;
89             if (frame - frame_start < offset || frame_end - frame < count)
90                 return AVERROR_INVALIDDATA;
91             av_memcpy_backptr(frame, offset, count);
92             frame += count;
93         } else {
94             *frame++ = bytestream2_get_byte(gb);
95             *frame++ = bytestream2_get_byte(gb);
96         }
97         mask <<= 1;
98     }
99
100     return 0;
101 }
102
103 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
104 {
105     const uint8_t *frame_start = frame;
106     const uint8_t *frame_end   = frame + width * height;
107     int mask = 0x10000, bitbuf = 0;
108     int v, offset, count, segments;
109
110     segments = bytestream2_get_le16(gb);
111     while (segments--) {
112         if (bytestream2_get_bytes_left(gb) < 2)
113             return AVERROR_INVALIDDATA;
114         if (mask == 0x10000) {
115             bitbuf = bytestream2_get_le16u(gb);
116             mask = 1;
117         }
118         if (frame_end - frame < 2)
119             return AVERROR_INVALIDDATA;
120         if (bitbuf & mask) {
121             v = bytestream2_get_le16(gb);
122             offset = (v & 0x1FFF) << 1;
123             count = ((v >> 13) + 2) << 1;
124             if (frame - frame_start < offset || frame_end - frame < count)
125                 return AVERROR_INVALIDDATA;
126             av_memcpy_backptr(frame, offset, count);
127             frame += count;
128         } else if (bitbuf & (mask << 1)) {
129             frame += bytestream2_get_le16(gb);
130         } else {
131             *frame++ = bytestream2_get_byte(gb);
132             *frame++ = bytestream2_get_byte(gb);
133         }
134         mask <<= 2;
135     }
136
137     return 0;
138 }
139
140 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
141 {
142     const uint8_t *frame_start = frame;
143     const uint8_t *frame_end   = frame + width * height;
144     int mask = 0x10000, bitbuf = 0;
145     int i, v, offset, count, segments;
146
147     segments = bytestream2_get_le16(gb);
148     while (segments--) {
149         if (bytestream2_get_bytes_left(gb) < 2)
150             return AVERROR_INVALIDDATA;
151         if (mask == 0x10000) {
152             bitbuf = bytestream2_get_le16u(gb);
153             mask = 1;
154         }
155
156         if (bitbuf & mask) {
157             v = bytestream2_get_le16(gb);
158             offset = (v & 0x1FFF) << 2;
159             count = ((v >> 13) + 2) << 1;
160             if (frame - frame_start < offset || frame_end - frame < count*2 + width)
161                 return AVERROR_INVALIDDATA;
162             for (i = 0; i < count; i++) {
163                 frame[0] = frame[1] =
164                 frame[width] = frame[width + 1] = frame[-offset];
165
166                 frame += 2;
167             }
168         } else if (bitbuf & (mask << 1)) {
169             v = bytestream2_get_le16(gb)*2;
170             if (frame - frame_end < v)
171                 return AVERROR_INVALIDDATA;
172             frame += v;
173         } else {
174             if (frame_end - frame < width + 3)
175                 return AVERROR_INVALIDDATA;
176             frame[0] = frame[1] =
177             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
178             frame += 2;
179             frame[0] = frame[1] =
180             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
181             frame += 2;
182         }
183         mask <<= 2;
184     }
185
186     return 0;
187 }
188
189 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
190 {
191     uint8_t *line_ptr;
192     int count, lines, segments;
193
194     count = bytestream2_get_le16(gb);
195     if (count >= height)
196         return AVERROR_INVALIDDATA;
197     frame += width * count;
198     lines = bytestream2_get_le16(gb);
199     if (count + lines > height)
200         return AVERROR_INVALIDDATA;
201
202     while (lines--) {
203         if (bytestream2_get_bytes_left(gb) < 1)
204             return AVERROR_INVALIDDATA;
205         line_ptr = frame;
206         frame += width;
207         segments = bytestream2_get_byteu(gb);
208         while (segments--) {
209             if (frame - line_ptr <= bytestream2_peek_byte(gb))
210                 return AVERROR_INVALIDDATA;
211             line_ptr += bytestream2_get_byte(gb);
212             count = (int8_t)bytestream2_get_byte(gb);
213             if (count >= 0) {
214                 if (frame - line_ptr < count)
215                     return AVERROR_INVALIDDATA;
216                 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
217                     return AVERROR_INVALIDDATA;
218             } else {
219                 count = -count;
220                 if (frame - line_ptr < count)
221                     return AVERROR_INVALIDDATA;
222                 memset(line_ptr, bytestream2_get_byte(gb), count);
223             }
224             line_ptr += count;
225         }
226     }
227
228     return 0;
229 }
230
231 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
232 {
233     const uint8_t *frame_end   = frame + width * height;
234     uint8_t *line_ptr;
235     int count, i, v, lines, segments;
236     int y = 0;
237
238     lines = bytestream2_get_le16(gb);
239     if (lines > height)
240         return AVERROR_INVALIDDATA;
241
242     while (lines--) {
243         if (bytestream2_get_bytes_left(gb) < 2)
244             return AVERROR_INVALIDDATA;
245         segments = bytestream2_get_le16u(gb);
246         while ((segments & 0xC000) == 0xC000) {
247             unsigned skip_lines = -(int16_t)segments;
248             unsigned delta = -((int16_t)segments * width);
249             if (frame_end - frame <= delta || y + lines + skip_lines > height)
250                 return AVERROR_INVALIDDATA;
251             frame    += delta;
252             y        += skip_lines;
253             segments = bytestream2_get_le16(gb);
254         }
255         if (segments & 0x8000) {
256             frame[width - 1] = segments & 0xFF;
257             segments = bytestream2_get_le16(gb);
258         }
259         line_ptr = frame;
260         if (frame_end - frame < width)
261             return AVERROR_INVALIDDATA;
262         frame += width;
263         y++;
264         while (segments--) {
265             if (frame - line_ptr <= bytestream2_peek_byte(gb))
266                 return AVERROR_INVALIDDATA;
267             line_ptr += bytestream2_get_byte(gb);
268             count = (int8_t)bytestream2_get_byte(gb);
269             if (count >= 0) {
270                 if (frame - line_ptr < count * 2)
271                     return AVERROR_INVALIDDATA;
272                 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
273                     return AVERROR_INVALIDDATA;
274                 line_ptr += count * 2;
275             } else {
276                 count = -count;
277                 if (frame - line_ptr < count * 2)
278                     return AVERROR_INVALIDDATA;
279                 v = bytestream2_get_le16(gb);
280                 for (i = 0; i < count; i++)
281                     bytestream_put_le16(&line_ptr, v);
282             }
283         }
284     }
285
286     return 0;
287 }
288
289 static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
290 {
291     const uint8_t *frame_end = frame + width * height;
292     int segments = bytestream2_get_le32(gb);
293     int skip, copy;
294
295     while (segments--) {
296         if (bytestream2_get_bytes_left(gb) < 2)
297             return AVERROR_INVALIDDATA;
298         copy = bytestream2_get_byteu(gb) * 2;
299         skip = bytestream2_get_byteu(gb) * 2;
300         if (frame_end - frame < copy + skip ||
301             bytestream2_get_bytes_left(gb) < copy)
302             return AVERROR_INVALIDDATA;
303         frame += skip;
304         bytestream2_get_buffer(gb, frame, copy);
305         frame += copy;
306     }
307
308     return 0;
309 }
310
311 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
312 {
313     memset(frame, 0, width * height);
314     return 0;
315 }
316
317
318 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
319
320 static const chunk_decoder decoder[8] = {
321     decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
322     decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
323 };
324
325 static const char* chunk_name[8] = {
326     "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
327 };
328
329 static int dfa_decode_frame(AVCodecContext *avctx,
330                             void *data, int *got_frame,
331                             AVPacket *avpkt)
332 {
333     AVFrame *frame = data;
334     DfaContext *s = avctx->priv_data;
335     GetByteContext gb;
336     const uint8_t *buf = avpkt->data;
337     uint32_t chunk_type, chunk_size;
338     uint8_t *dst;
339     int ret;
340     int i, pal_elems;
341
342     if ((ret = ff_get_buffer(avctx, frame, 0))) {
343         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
344         return ret;
345     }
346
347     bytestream2_init(&gb, avpkt->data, avpkt->size);
348     while (bytestream2_get_bytes_left(&gb) > 0) {
349         bytestream2_skip(&gb, 4);
350         chunk_size = bytestream2_get_le32(&gb);
351         chunk_type = bytestream2_get_le32(&gb);
352         if (!chunk_type)
353             break;
354         if (chunk_type == 1) {
355             pal_elems = FFMIN(chunk_size / 3, 256);
356             for (i = 0; i < pal_elems; i++) {
357                 s->pal[i] = bytestream2_get_be24(&gb) << 2;
358                 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
359             }
360             frame->palette_has_changed = 1;
361         } else if (chunk_type <= 9) {
362             if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
363                 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
364                        chunk_name[chunk_type - 2]);
365                 return AVERROR_INVALIDDATA;
366             }
367         } else {
368             av_log(avctx, AV_LOG_WARNING,
369                    "Ignoring unknown chunk type %"PRIu32"\n",
370                    chunk_type);
371         }
372         buf += chunk_size;
373     }
374
375     buf = s->frame_buf;
376     dst = frame->data[0];
377     for (i = 0; i < avctx->height; i++) {
378         memcpy(dst, buf, avctx->width);
379         dst += frame->linesize[0];
380         buf += avctx->width;
381     }
382     memcpy(frame->data[1], s->pal, sizeof(s->pal));
383
384     *got_frame = 1;
385
386     return avpkt->size;
387 }
388
389 static av_cold int dfa_decode_end(AVCodecContext *avctx)
390 {
391     DfaContext *s = avctx->priv_data;
392
393     av_freep(&s->frame_buf);
394
395     return 0;
396 }
397
398 AVCodec ff_dfa_decoder = {
399     .name           = "dfa",
400     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
401     .type           = AVMEDIA_TYPE_VIDEO,
402     .id             = AV_CODEC_ID_DFA,
403     .priv_data_size = sizeof(DfaContext),
404     .init           = dfa_decode_init,
405     .close          = dfa_decode_end,
406     .decode         = dfa_decode_frame,
407     .capabilities   = CODEC_CAP_DR1,
408 };