4 * Copyright (c) 2011 Konstantin Shishkov
6 * This file is part of Libav.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
26 static av_cold int decode_init(AVCodecContext *avctx)
28 avctx->pix_fmt = PIX_FMT_YUV420P;
29 avctx->coded_frame = avcodec_alloc_frame();
30 if (!avctx->coded_frame)
31 return AVERROR(ENOMEM);
36 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
40 AVFrame *pic = avctx->coded_frame;
41 const uint8_t *src = avpkt->data;
42 uint8_t *Y1, *Y2, *U, *V;
46 avctx->release_buffer(avctx, pic);
48 if (avpkt->size < avctx->width * avctx->height * 3 / 2 + 16) {
49 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
50 return AVERROR_INVALIDDATA;
54 if ((ret = avctx->get_buffer(avctx, pic)) < 0)
57 pic->pict_type = AV_PICTURE_TYPE_I;
60 if (AV_RL32(src) != 0x01000002) {
61 av_log_ask_for_sample(avctx, "Unknown frame header %X\n", AV_RL32(src));
62 return AVERROR_PATCHWELCOME;
67 Y2 = pic->data[0] + pic->linesize[0];
70 for (h = 0; h < avctx->height; h += 2) {
71 for (w = 0; w < avctx->width; w += 2) {
72 AV_WN16A(Y1 + w, AV_RN16A(src));
73 AV_WN16A(Y2 + w, AV_RN16A(src + 2));
74 U[w >> 1] = src[4] + 0x80;
75 V[w >> 1] = src[5] + 0x80;
78 Y1 += pic->linesize[0] << 1;
79 Y2 += pic->linesize[0] << 1;
80 U += pic->linesize[1];
81 V += pic->linesize[2];
84 *data_size = sizeof(AVFrame);
85 *(AVFrame*)data = *pic;
90 static av_cold int decode_close(AVCodecContext *avctx)
92 AVFrame *pic = avctx->coded_frame;
94 avctx->release_buffer(avctx, pic);
95 av_freep(&avctx->coded_frame);
100 AVCodec ff_dxtory_decoder = {
102 .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
103 .type = AVMEDIA_TYPE_VIDEO,
104 .id = CODEC_ID_DXTORY,
106 .close = decode_close,
107 .decode = decode_frame,
108 .capabilities = CODEC_CAP_DR1,