4 * Copyright (c) 2011 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
28 #define BITSTREAM_READER_LE
30 #include "bytestream.h"
35 static void do_vflip(AVCodecContext *avctx, AVFrame *pic, int vflip)
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];
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];
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];
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)
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;
84 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
87 do_vflip(avctx, pic, vflipped);
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];
96 do_vflip(avctx, pic, vflipped);
101 static int dxtory_decode_v1_410(AVCodecContext *avctx, AVFrame *pic,
102 const uint8_t *src, int src_size,
106 uint8_t *Y1, *Y2, *Y3, *Y4, *U, *V;
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;
114 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
115 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
118 do_vflip(avctx, pic, vflipped);
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;
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;
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];
144 do_vflip(avctx, pic, vflipped);
149 static int dxtory_decode_v1_420(AVCodecContext *avctx, AVFrame *pic,
150 const uint8_t *src, int src_size,
154 uint8_t *Y1, *Y2, *U, *V;
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;
162 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
163 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
166 do_vflip(avctx, pic, vflipped);
169 Y2 = pic->data[0] + pic->linesize[0];
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;
180 Y1 += pic->linesize[0] << 1;
181 Y2 += pic->linesize[0] << 1;
182 U += pic->linesize[1];
183 V += pic->linesize[2];
186 do_vflip(avctx, pic, vflipped);
191 static int dxtory_decode_v1_444(AVCodecContext *avctx, AVFrame *pic,
192 const uint8_t *src, int src_size,
199 if (src_size < avctx->width * avctx->height * 3LL) {
200 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
201 return AVERROR_INVALIDDATA;
204 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
205 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
208 do_vflip(avctx, pic, vflipped);
213 for (h = 0; h < avctx->height; h++) {
214 for (w = 0; w < avctx->width; w++) {
216 U[w] = *src++ ^ 0x80;
217 V[w] = *src++ ^ 0x80;
219 Y += pic->linesize[0];
220 U += pic->linesize[1];
221 V += pic->linesize[2];
224 do_vflip(avctx, pic, vflipped);
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 };
233 static inline uint8_t decode_sym(GetBitContext *gb, uint8_t lru[8])
237 c = get_unary(gb, 0, 8);
239 val = get_bits(gb, 8);
240 memmove(lru + 1, lru, sizeof(*lru) * (8 - 1));
243 memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
250 static int check_slice_size(AVCodecContext *avctx,
251 const uint8_t *src, int src_size,
252 int slice_size, int off)
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;
262 if (slice_size <= 16) {
263 av_log(avctx, AV_LOG_ERROR, "invalid slice size %d\n",
265 return AVERROR_INVALIDDATA;
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);
278 static int load_buffer(AVCodecContext *avctx,
279 const uint8_t *src, int src_size,
281 int *nslices, int *off)
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;
292 avpriv_request_sample(avctx, "%d slices for %dx%d", *nslices,
293 avctx->width, avctx->height);
294 return AVERROR_PATCHWELCOME;
300 static inline uint8_t decode_sym_565(GetBitContext *gb, uint8_t lru[8],
305 c = get_unary(gb, 0, bits);
307 val = get_bits(gb, bits);
308 memmove(lru + 1, lru, sizeof(*lru) * (6 - 1));
311 memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
318 typedef int (*decode_slice_func)(GetBitContext *gb, AVFrame *frame,
319 int line, int height, uint8_t lru[3][8]);
321 typedef void (*setup_lru_func)(uint8_t lru[3][8]);
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,
330 GetByteContext gb, gb_check;
332 int nslices, slice, line = 0;
333 uint32_t off, slice_size;
338 ret = load_buffer(avctx, src, src_size, &gb, &nslices, &off);
344 for (slice = 0; slice < nslices; slice++) {
345 slice_size = bytestream2_get_le32(&gb_check);
347 if (slice_size <= 16 + (avctx->height * avctx->width / (8 * nslices)))
348 return AVERROR_INVALIDDATA;
349 off_check += slice_size;
352 if (off_check - avctx->discard_damaged_percentage*off_check/100 > src_size)
353 return AVERROR_INVALIDDATA;
355 avctx->pix_fmt = fmt;
356 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
359 do_vflip(avctx, pic, vflipped);
361 for (slice = 0; slice < nslices; slice++) {
362 slice_size = bytestream2_get_le32(&gb);
366 ret = check_slice_size(avctx, src, src_size, slice_size, off);
370 if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
373 line += decode_slice(&gb2, pic, line, avctx->height - line, lru);
378 if (avctx->height - line) {
379 avpriv_request_sample(avctx, "Not enough slice data available");
382 do_vflip(avctx, pic, vflipped);
388 static int dx2_decode_slice_5x5(GetBitContext *gb, AVFrame *frame,
389 int line, int left, uint8_t lru[3][8],
394 int width = frame->width;
395 int stride = frame->linesize[0];
396 uint8_t *dst = frame->data[0] + stride * line;
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);
414 static void setup_lru_555(uint8_t lru[3][8])
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));
421 static void setup_lru_565(uint8_t lru[3][8])
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));
428 static int dx2_decode_slice_555(GetBitContext *gb, AVFrame *frame,
429 int line, int left, uint8_t lru[3][8])
431 return dx2_decode_slice_5x5(gb, frame, line, left, lru, 0);
434 static int dx2_decode_slice_565(GetBitContext *gb, AVFrame *frame,
435 int line, int left, uint8_t lru[3][8])
437 return dx2_decode_slice_5x5(gb, frame, line, left, lru, 1);
440 static int dxtory_decode_v2_565(AVCodecContext *avctx, AVFrame *pic,
441 const uint8_t *src, int src_size, int is_565,
444 enum AVPixelFormat fmt = AV_PIX_FMT_RGB24;
446 return dxtory_decode_v2(avctx, pic, src, src_size,
447 dx2_decode_slice_565,
451 return dxtory_decode_v2(avctx, pic, src, src_size,
452 dx2_decode_slice_555,
457 static int dx2_decode_slice_rgb(GetBitContext *gb, AVFrame *frame,
458 int line, int left, uint8_t lru[3][8])
461 int width = frame->width;
462 int stride = frame->linesize[0];
463 uint8_t *dst = frame->data[0] + stride * line;
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]);
478 static void default_setup_lru(uint8_t lru[3][8])
482 for (i = 0; i < 3; i++)
483 memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
486 static int dxtory_decode_v2_rgb(AVCodecContext *avctx, AVFrame *pic,
487 const uint8_t *src, int src_size,
490 return dxtory_decode_v2(avctx, pic, src, src_size,
491 dx2_decode_slice_rgb,
493 AV_PIX_FMT_BGR24, vflipped);
496 static int dx2_decode_slice_410(GetBitContext *gb, AVFrame *frame,
501 int width = frame->width;
503 int ystride = frame->linesize[0];
504 int ustride = frame->linesize[1];
505 int vstride = frame->linesize[2];
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;
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;
529 static int dxtory_decode_v2_410(AVCodecContext *avctx, AVFrame *pic,
530 const uint8_t *src, int src_size,
533 return dxtory_decode_v2(avctx, pic, src, src_size,
534 dx2_decode_slice_410,
536 AV_PIX_FMT_YUV410P, vflipped);
539 static int dx2_decode_slice_420(GetBitContext *gb, AVFrame *frame,
545 int width = frame->width;
547 int ystride = frame->linesize[0];
548 int ustride = frame->linesize[1];
549 int vstride = frame->linesize[2];
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;
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;
574 static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
575 const uint8_t *src, int src_size,
578 return dxtory_decode_v2(avctx, pic, src, src_size,
579 dx2_decode_slice_420,
581 AV_PIX_FMT_YUV420P, vflipped);
584 static int dx2_decode_slice_444(GetBitContext *gb, AVFrame *frame,
590 int width = frame->width;
592 int ystride = frame->linesize[0];
593 int ustride = frame->linesize[1];
594 int vstride = frame->linesize[2];
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;
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;
615 static int dxtory_decode_v2_444(AVCodecContext *avctx, AVFrame *pic,
616 const uint8_t *src, int src_size,
619 return dxtory_decode_v2(avctx, pic, src, src_size,
620 dx2_decode_slice_444,
622 AV_PIX_FMT_YUV444P, vflipped);
625 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
629 const uint8_t *src = avpkt->data;
633 if (avpkt->size < 16) {
634 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
635 return AVERROR_INVALIDDATA;
639 vflipped = !!(type & 0x20);
644 ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
645 AV_PIX_FMT_BGR24, 3, vflipped);
649 ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16, vflipped);
653 ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16, vflipped);
657 ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16, vflipped);
661 ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16, vflipped);
665 ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16, vflipped);
669 ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16, vflipped);
673 ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16, vflipped);
677 ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
678 AV_PIX_FMT_RGB565LE, 2, vflipped);
682 ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1, vflipped);
688 ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
689 AV_PIX_FMT_RGB555LE, 2, vflipped);
695 ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0, vflipped);
698 avpriv_request_sample(avctx, "Frame header %"PRIX32, type);
699 return AVERROR_PATCHWELCOME;
705 pic->pict_type = AV_PICTURE_TYPE_I;
712 AVCodec ff_dxtory_decoder = {
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,