]> git.sesse.net Git - ffmpeg/blob - libavcodec/dxtory.c
avcodec/dxtory: add support for vertically flipped frames
[ffmpeg] / libavcodec / dxtory.c
1 /*
2  * Dxtory decoder
3  *
4  * Copyright (c) 2011 Konstantin Shishkov
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
27
28 #define BITSTREAM_READER_LE
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 #include "unary.h"
34
35 static void do_vflip(AVCodecContext *avctx, AVFrame *pic, int vflip)
36 {
37     if (!vflip)
38         return;
39
40     switch (pic->format) {
41     case AV_PIX_FMT_YUV444P:
42         pic->data[1] += (avctx->height - 1) * pic->linesize[1];
43         pic->linesize[1] = -pic->linesize[1];
44         pic->data[2] += (avctx->height - 1) * pic->linesize[2];
45         pic->linesize[2] = -pic->linesize[2];
46     case AV_PIX_FMT_BGR24:
47     case AV_PIX_FMT_RGB24:
48         pic->data[0] += (avctx->height - 1) * pic->linesize[0];
49         pic->linesize[0] = -pic->linesize[0];
50         break;
51     case AV_PIX_FMT_YUV410P:
52         pic->data[0] += (avctx->height - 1) * pic->linesize[0];
53         pic->linesize[0] = -pic->linesize[0];
54         pic->data[1] += ((avctx->height >> 2) - 1) * pic->linesize[1];
55         pic->linesize[1] = -pic->linesize[1];
56         pic->data[2] += ((avctx->height >> 2) - 1) * pic->linesize[2];
57         pic->linesize[2] = -pic->linesize[2];
58         break;
59     case AV_PIX_FMT_YUV420P:
60         pic->data[0] += (avctx->height - 1) * pic->linesize[0];
61         pic->linesize[0] = -pic->linesize[0];
62         pic->data[1] += ((avctx->height >> 1) - 1) * pic->linesize[1];
63         pic->linesize[1] = -pic->linesize[1];
64         pic->data[2] += ((avctx->height >> 1) - 1) * pic->linesize[2];
65         pic->linesize[2] = -pic->linesize[2];
66         break;
67     }
68 }
69
70 static int dxtory_decode_v1_rgb(AVCodecContext *avctx, AVFrame *pic,
71                                 const uint8_t *src, int src_size,
72                                 int id, int bpp, uint32_t vflipped)
73 {
74     int h;
75     uint8_t *dst;
76     int ret;
77
78     if (src_size < avctx->width * avctx->height * (int64_t)bpp) {
79         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
80         return AVERROR_INVALIDDATA;
81     }
82
83     avctx->pix_fmt = id;
84     if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
85         return ret;
86
87     do_vflip(avctx, pic, vflipped);
88
89     dst = pic->data[0];
90     for (h = 0; h < avctx->height; h++) {
91         memcpy(dst, src, avctx->width * bpp);
92         src += avctx->width * bpp;
93         dst += pic->linesize[0];
94     }
95
96     do_vflip(avctx, pic, vflipped);
97
98     return 0;
99 }
100
101 static int dxtory_decode_v1_410(AVCodecContext *avctx, AVFrame *pic,
102                                 const uint8_t *src, int src_size,
103                                 uint32_t vflipped)
104 {
105     int h, w;
106     uint8_t *Y1, *Y2, *Y3, *Y4, *U, *V;
107     int ret;
108
109     if (src_size < FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4) * 9LL / 8) {
110         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
111         return AVERROR_INVALIDDATA;
112     }
113
114     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
115     if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
116         return ret;
117
118     do_vflip(avctx, pic, vflipped);
119
120     Y1 = pic->data[0];
121     Y2 = pic->data[0] + pic->linesize[0];
122     Y3 = pic->data[0] + pic->linesize[0] * 2;
123     Y4 = pic->data[0] + pic->linesize[0] * 3;
124     U  = pic->data[1];
125     V  = pic->data[2];
126     for (h = 0; h < avctx->height; h += 4) {
127         for (w = 0; w < avctx->width; w += 4) {
128             AV_COPY32U(Y1 + w, src);
129             AV_COPY32U(Y2 + w, src + 4);
130             AV_COPY32U(Y3 + w, src + 8);
131             AV_COPY32U(Y4 + w, src + 12);
132             U[w >> 2] = src[16] + 0x80;
133             V[w >> 2] = src[17] + 0x80;
134             src += 18;
135         }
136         Y1 += pic->linesize[0] << 2;
137         Y2 += pic->linesize[0] << 2;
138         Y3 += pic->linesize[0] << 2;
139         Y4 += pic->linesize[0] << 2;
140         U  += pic->linesize[1];
141         V  += pic->linesize[2];
142     }
143
144     do_vflip(avctx, pic, vflipped);
145
146     return 0;
147 }
148
149 static int dxtory_decode_v1_420(AVCodecContext *avctx, AVFrame *pic,
150                                 const uint8_t *src, int src_size,
151                                 uint32_t vflipped)
152 {
153     int h, w;
154     uint8_t *Y1, *Y2, *U, *V;
155     int ret;
156
157     if (src_size < FFALIGN(avctx->width, 2) * FFALIGN(avctx->height, 2) * 3LL / 2) {
158         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
159         return AVERROR_INVALIDDATA;
160     }
161
162     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
163     if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
164         return ret;
165
166     do_vflip(avctx, pic, vflipped);
167
168     Y1 = pic->data[0];
169     Y2 = pic->data[0] + pic->linesize[0];
170     U  = pic->data[1];
171     V  = pic->data[2];
172     for (h = 0; h < avctx->height; h += 2) {
173         for (w = 0; w < avctx->width; w += 2) {
174             AV_COPY16(Y1 + w, src);
175             AV_COPY16(Y2 + w, src + 2);
176             U[w >> 1] = src[4] + 0x80;
177             V[w >> 1] = src[5] + 0x80;
178             src += 6;
179         }
180         Y1 += pic->linesize[0] << 1;
181         Y2 += pic->linesize[0] << 1;
182         U  += pic->linesize[1];
183         V  += pic->linesize[2];
184     }
185
186     do_vflip(avctx, pic, vflipped);
187
188     return 0;
189 }
190
191 static int dxtory_decode_v1_444(AVCodecContext *avctx, AVFrame *pic,
192                                 const uint8_t *src, int src_size,
193                                 uint32_t vflipped)
194 {
195     int h, w;
196     uint8_t *Y, *U, *V;
197     int ret;
198
199     if (src_size < avctx->width * avctx->height * 3LL) {
200         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
201         return AVERROR_INVALIDDATA;
202     }
203
204     avctx->pix_fmt = AV_PIX_FMT_YUV444P;
205     if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
206         return ret;
207
208     do_vflip(avctx, pic, vflipped);
209
210     Y = pic->data[0];
211     U = pic->data[1];
212     V = pic->data[2];
213     for (h = 0; h < avctx->height; h++) {
214         for (w = 0; w < avctx->width; w++) {
215             Y[w] = *src++;
216             U[w] = *src++ ^ 0x80;
217             V[w] = *src++ ^ 0x80;
218         }
219         Y += pic->linesize[0];
220         U += pic->linesize[1];
221         V += pic->linesize[2];
222     }
223
224     do_vflip(avctx, pic, vflipped);
225
226     return 0;
227 }
228
229 static const uint8_t def_lru[8] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xFF };
230 static const uint8_t def_lru_555[8] = { 0x00, 0x08, 0x10, 0x18, 0x1F };
231 static const uint8_t def_lru_565[8] = { 0x00, 0x08, 0x10, 0x20, 0x30, 0x3F };
232
233 static inline uint8_t decode_sym(GetBitContext *gb, uint8_t lru[8])
234 {
235     uint8_t c, val;
236
237     c = get_unary(gb, 0, 8);
238     if (!c) {
239         val = get_bits(gb, 8);
240         memmove(lru + 1, lru, sizeof(*lru) * (8 - 1));
241     } else {
242         val = lru[c - 1];
243         memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
244     }
245     lru[0] = val;
246
247     return val;
248 }
249
250 static int check_slice_size(AVCodecContext *avctx,
251                             const uint8_t *src, int src_size,
252                             int slice_size, int off)
253 {
254     int cur_slice_size;
255
256     if (slice_size > src_size - off) {
257         av_log(avctx, AV_LOG_ERROR,
258                "invalid slice size %d (only %d bytes left)\n",
259                slice_size, src_size - off);
260         return AVERROR_INVALIDDATA;
261     }
262     if (slice_size <= 16) {
263         av_log(avctx, AV_LOG_ERROR, "invalid slice size %d\n",
264                slice_size);
265         return AVERROR_INVALIDDATA;
266     }
267
268     cur_slice_size = AV_RL32(src + off);
269     if (cur_slice_size != slice_size - 16) {
270         av_log(avctx, AV_LOG_ERROR,
271                "Slice sizes mismatch: got %d instead of %d\n",
272                cur_slice_size, slice_size - 16);
273     }
274
275     return 0;
276 }
277
278 static int load_buffer(AVCodecContext *avctx,
279                        const uint8_t *src, int src_size,
280                        GetByteContext *gb,
281                        int *nslices, int *off)
282 {
283     bytestream2_init(gb, src, src_size);
284     *nslices = bytestream2_get_le16(gb);
285     *off = FFALIGN(*nslices * 4 + 2, 16);
286     if (src_size < *off) {
287         av_log(avctx, AV_LOG_ERROR, "no slice data\n");
288         return AVERROR_INVALIDDATA;
289     }
290
291     if (!*nslices) {
292         avpriv_request_sample(avctx, "%d slices for %dx%d", *nslices,
293                               avctx->width, avctx->height);
294         return AVERROR_PATCHWELCOME;
295     }
296
297     return 0;
298 }
299
300 static inline uint8_t decode_sym_565(GetBitContext *gb, uint8_t lru[8],
301                                      int bits)
302 {
303     uint8_t c, val;
304
305     c = get_unary(gb, 0, bits);
306     if (!c) {
307         val = get_bits(gb, bits);
308         memmove(lru + 1, lru, sizeof(*lru) * (6 - 1));
309     } else {
310         val = lru[c - 1];
311         memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
312     }
313     lru[0] = val;
314
315     return val;
316 }
317
318 typedef int (*decode_slice_func)(GetBitContext *gb, AVFrame *frame,
319                                  int line, int height, uint8_t lru[3][8]);
320
321 typedef void (*setup_lru_func)(uint8_t lru[3][8]);
322
323 static int dxtory_decode_v2(AVCodecContext *avctx, AVFrame *pic,
324                             const uint8_t *src, int src_size,
325                             decode_slice_func decode_slice,
326                             setup_lru_func setup_lru,
327                             enum AVPixelFormat fmt,
328                             uint32_t vflipped)
329 {
330     GetByteContext gb, gb_check;
331     GetBitContext  gb2;
332     int nslices, slice, line = 0;
333     uint32_t off, slice_size;
334     uint64_t off_check;
335     uint8_t lru[3][8];
336     int ret;
337
338     ret = load_buffer(avctx, src, src_size, &gb, &nslices, &off);
339     if (ret < 0)
340         return ret;
341
342     off_check = off;
343     gb_check = gb;
344     for (slice = 0; slice < nslices; slice++) {
345         slice_size = bytestream2_get_le32(&gb_check);
346
347         if (slice_size <= 16 + (avctx->height * avctx->width / (8 * nslices)))
348             return AVERROR_INVALIDDATA;
349         off_check += slice_size;
350     }
351
352     if (off_check - avctx->discard_damaged_percentage*off_check/100 > src_size)
353         return AVERROR_INVALIDDATA;
354
355     avctx->pix_fmt = fmt;
356     if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
357         return ret;
358
359     do_vflip(avctx, pic, vflipped);
360
361     for (slice = 0; slice < nslices; slice++) {
362         slice_size = bytestream2_get_le32(&gb);
363
364         setup_lru(lru);
365
366         ret = check_slice_size(avctx, src, src_size, slice_size, off);
367         if (ret < 0)
368             return ret;
369
370         if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
371             return ret;
372
373         line += decode_slice(&gb2, pic, line, avctx->height - line, lru);
374
375         off += slice_size;
376     }
377
378     if (avctx->height - line) {
379         avpriv_request_sample(avctx, "Not enough slice data available");
380     }
381
382     do_vflip(avctx, pic, vflipped);
383
384     return 0;
385 }
386
387 av_always_inline
388 static int dx2_decode_slice_5x5(GetBitContext *gb, AVFrame *frame,
389                                 int line, int left, uint8_t lru[3][8],
390                                 int is_565)
391 {
392     int x, y;
393     int r, g, b;
394     int width    = frame->width;
395     int stride   = frame->linesize[0];
396     uint8_t *dst = frame->data[0] + stride * line;
397
398     for (y = 0; y < left && get_bits_left(gb) > 6 * width; y++) {
399         for (x = 0; x < width; x++) {
400             b = decode_sym_565(gb, lru[0], 5);
401             g = decode_sym_565(gb, lru[1], is_565 ? 6 : 5);
402             r = decode_sym_565(gb, lru[2], 5);
403             dst[x * 3 + 0] = (r << 3) | (r >> 2);
404             dst[x * 3 + 1] = is_565 ? (g << 2) | (g >> 4) : (g << 3) | (g >> 2);
405             dst[x * 3 + 2] = (b << 3) | (b >> 2);
406         }
407
408         dst += stride;
409     }
410
411     return y;
412 }
413
414 static void setup_lru_555(uint8_t lru[3][8])
415 {
416     memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
417     memcpy(lru[1], def_lru_555, 8 * sizeof(*def_lru));
418     memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
419 }
420
421 static void setup_lru_565(uint8_t lru[3][8])
422 {
423     memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
424     memcpy(lru[1], def_lru_565, 8 * sizeof(*def_lru));
425     memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
426 }
427
428 static int dx2_decode_slice_555(GetBitContext *gb, AVFrame *frame,
429                                 int line, int left, uint8_t lru[3][8])
430 {
431     return dx2_decode_slice_5x5(gb, frame, line, left, lru, 0);
432 }
433
434 static int dx2_decode_slice_565(GetBitContext *gb, AVFrame *frame,
435                                 int line, int left, uint8_t lru[3][8])
436 {
437     return dx2_decode_slice_5x5(gb, frame, line, left, lru, 1);
438 }
439
440 static int dxtory_decode_v2_565(AVCodecContext *avctx, AVFrame *pic,
441                                 const uint8_t *src, int src_size, int is_565,
442                                 uint32_t vflipped)
443 {
444     enum AVPixelFormat fmt = AV_PIX_FMT_RGB24;
445     if (is_565)
446         return dxtory_decode_v2(avctx, pic, src, src_size,
447                                 dx2_decode_slice_565,
448                                 setup_lru_565,
449                                 fmt, vflipped);
450     else
451         return dxtory_decode_v2(avctx, pic, src, src_size,
452                                 dx2_decode_slice_555,
453                                 setup_lru_555,
454                                 fmt, vflipped);
455 }
456
457 static int dx2_decode_slice_rgb(GetBitContext *gb, AVFrame *frame,
458                                 int line, int left, uint8_t lru[3][8])
459 {
460     int x, y;
461     int width    = frame->width;
462     int stride   = frame->linesize[0];
463     uint8_t *dst = frame->data[0] + stride * line;
464
465     for (y = 0; y < left && get_bits_left(gb) > 6 * width; y++) {
466         for (x = 0; x < width; x++) {
467             dst[x * 3 + 0] = decode_sym(gb, lru[0]);
468             dst[x * 3 + 1] = decode_sym(gb, lru[1]);
469             dst[x * 3 + 2] = decode_sym(gb, lru[2]);
470         }
471
472         dst += stride;
473     }
474
475     return y;
476 }
477
478 static void default_setup_lru(uint8_t lru[3][8])
479 {
480     int i;
481
482     for (i = 0; i < 3; i++)
483         memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
484 }
485
486 static int dxtory_decode_v2_rgb(AVCodecContext *avctx, AVFrame *pic,
487                                 const uint8_t *src, int src_size,
488                                 uint32_t vflipped)
489 {
490     return dxtory_decode_v2(avctx, pic, src, src_size,
491                             dx2_decode_slice_rgb,
492                             default_setup_lru,
493                             AV_PIX_FMT_BGR24, vflipped);
494 }
495
496 static int dx2_decode_slice_410(GetBitContext *gb, AVFrame *frame,
497                                 int line, int left,
498                                 uint8_t lru[3][8])
499 {
500     int x, y, i, j;
501     int width   = frame->width;
502
503     int ystride = frame->linesize[0];
504     int ustride = frame->linesize[1];
505     int vstride = frame->linesize[2];
506
507     uint8_t *Y  = frame->data[0] + ystride * line;
508     uint8_t *U  = frame->data[1] + (ustride >> 2) * line;
509     uint8_t *V  = frame->data[2] + (vstride >> 2) * line;
510
511     for (y = 0; y < left - 3 && get_bits_left(gb) > 9 * width; y += 4) {
512         for (x = 0; x < width; x += 4) {
513             for (j = 0; j < 4; j++)
514                 for (i = 0; i < 4; i++)
515                     Y[x + i + j * ystride] = decode_sym(gb, lru[0]);
516             U[x >> 2] = decode_sym(gb, lru[1]) ^ 0x80;
517             V[x >> 2] = decode_sym(gb, lru[2]) ^ 0x80;
518         }
519
520         Y += ystride << 2;
521         U += ustride;
522         V += vstride;
523     }
524
525     return y;
526 }
527
528
529 static int dxtory_decode_v2_410(AVCodecContext *avctx, AVFrame *pic,
530                                 const uint8_t *src, int src_size,
531                                 uint32_t vflipped)
532 {
533     return dxtory_decode_v2(avctx, pic, src, src_size,
534                             dx2_decode_slice_410,
535                             default_setup_lru,
536                             AV_PIX_FMT_YUV410P, vflipped);
537 }
538
539 static int dx2_decode_slice_420(GetBitContext *gb, AVFrame *frame,
540                                 int line, int left,
541                                 uint8_t lru[3][8])
542 {
543     int x, y;
544
545     int width    = frame->width;
546
547     int ystride = frame->linesize[0];
548     int ustride = frame->linesize[1];
549     int vstride = frame->linesize[2];
550
551     uint8_t *Y  = frame->data[0] + ystride * line;
552     uint8_t *U  = frame->data[1] + (ustride >> 1) * line;
553     uint8_t *V  = frame->data[2] + (vstride >> 1) * line;
554
555
556     for (y = 0; y < left - 1 && get_bits_left(gb) > 6 * width; y += 2) {
557         for (x = 0; x < width; x += 2) {
558             Y[x + 0 + 0 * ystride] = decode_sym(gb, lru[0]);
559             Y[x + 1 + 0 * ystride] = decode_sym(gb, lru[0]);
560             Y[x + 0 + 1 * ystride] = decode_sym(gb, lru[0]);
561             Y[x + 1 + 1 * ystride] = decode_sym(gb, lru[0]);
562             U[x >> 1] = decode_sym(gb, lru[1]) ^ 0x80;
563             V[x >> 1] = decode_sym(gb, lru[2]) ^ 0x80;
564         }
565
566         Y += ystride << 1;
567         U += ustride;
568         V += vstride;
569     }
570
571     return y;
572 }
573
574 static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
575                                 const uint8_t *src, int src_size,
576                                 uint32_t vflipped)
577 {
578     return dxtory_decode_v2(avctx, pic, src, src_size,
579                             dx2_decode_slice_420,
580                             default_setup_lru,
581                             AV_PIX_FMT_YUV420P, vflipped);
582 }
583
584 static int dx2_decode_slice_444(GetBitContext *gb, AVFrame *frame,
585                                 int line, int left,
586                                 uint8_t lru[3][8])
587 {
588     int x, y;
589
590     int width   = frame->width;
591
592     int ystride = frame->linesize[0];
593     int ustride = frame->linesize[1];
594     int vstride = frame->linesize[2];
595
596     uint8_t *Y  = frame->data[0] + ystride * line;
597     uint8_t *U  = frame->data[1] + ustride * line;
598     uint8_t *V  = frame->data[2] + vstride * line;
599
600     for (y = 0; y < left && get_bits_left(gb) > 6 * width; y++) {
601         for (x = 0; x < width; x++) {
602             Y[x] = decode_sym(gb, lru[0]);
603             U[x] = decode_sym(gb, lru[1]) ^ 0x80;
604             V[x] = decode_sym(gb, lru[2]) ^ 0x80;
605         }
606
607         Y += ystride;
608         U += ustride;
609         V += vstride;
610     }
611
612     return y;
613 }
614
615 static int dxtory_decode_v2_444(AVCodecContext *avctx, AVFrame *pic,
616                                 const uint8_t *src, int src_size,
617                                 uint32_t vflipped)
618 {
619     return dxtory_decode_v2(avctx, pic, src, src_size,
620                             dx2_decode_slice_444,
621                             default_setup_lru,
622                             AV_PIX_FMT_YUV444P, vflipped);
623 }
624
625 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
626                         AVPacket *avpkt)
627 {
628     AVFrame *pic = data;
629     const uint8_t *src = avpkt->data;
630     uint32_t type;
631     int vflipped, ret;
632
633     if (avpkt->size < 16) {
634         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
635         return AVERROR_INVALIDDATA;
636     }
637
638     type = AV_RB32(src);
639     vflipped = !!(type & 0x20);
640
641     switch (type) {
642     case 0x01000021:
643     case 0x01000001:
644         ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
645                                    AV_PIX_FMT_BGR24, 3, vflipped);
646         break;
647     case 0x01000029:
648     case 0x01000009:
649         ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16, vflipped);
650         break;
651     case 0x02000021:
652     case 0x02000001:
653         ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16, vflipped);
654         break;
655     case 0x02000029:
656     case 0x02000009:
657         ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16, vflipped);
658         break;
659     case 0x03000021:
660     case 0x03000001:
661         ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16, vflipped);
662         break;
663     case 0x03000029:
664     case 0x03000009:
665         ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16, vflipped);
666         break;
667     case 0x04000021:
668     case 0x04000001:
669         ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16, vflipped);
670         break;
671     case 0x04000029:
672     case 0x04000009:
673         ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16, vflipped);
674         break;
675     case 0x17000021:
676     case 0x17000001:
677         ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
678                                    AV_PIX_FMT_RGB565LE, 2, vflipped);
679         break;
680     case 0x17000029:
681     case 0x17000009:
682         ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1, vflipped);
683         break;
684     case 0x18000021:
685     case 0x19000021:
686     case 0x18000001:
687     case 0x19000001:
688         ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
689                                    AV_PIX_FMT_RGB555LE, 2, vflipped);
690         break;
691     case 0x18000029:
692     case 0x19000029:
693     case 0x18000009:
694     case 0x19000009:
695         ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0, vflipped);
696         break;
697     default:
698         avpriv_request_sample(avctx, "Frame header %"PRIX32, type);
699         return AVERROR_PATCHWELCOME;
700     }
701
702     if (ret)
703         return ret;
704
705     pic->pict_type = AV_PICTURE_TYPE_I;
706     pic->key_frame = 1;
707     *got_frame = 1;
708
709     return avpkt->size;
710 }
711
712 AVCodec ff_dxtory_decoder = {
713     .name           = "dxtory",
714     .long_name      = NULL_IF_CONFIG_SMALL("Dxtory"),
715     .type           = AVMEDIA_TYPE_VIDEO,
716     .id             = AV_CODEC_ID_DXTORY,
717     .decode         = decode_frame,
718     .capabilities   = AV_CODEC_CAP_DR1,
719 };