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