30983f8056d3cb5611bc844551d1e25069785268
[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 "avcodec.h"
24 #include "internal.h"
25 #include "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
27
28 static av_cold int decode_init(AVCodecContext *avctx)
29 {
30 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
31 avctx->coded_frame = avcodec_alloc_frame();
32 if (!avctx->coded_frame)
33 return AVERROR(ENOMEM);
34
35 return 0;
36 }
37
38 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
39 AVPacket *avpkt)
40 {
41 int h, w;
42 AVFrame *pic = avctx->coded_frame;
43 const uint8_t *src = avpkt->data;
44 uint8_t *Y1, *Y2, *U, *V;
45 int ret;
46
47 if (pic->data[0])
48 avctx->release_buffer(avctx, pic);
49
50 if (avpkt->size < avctx->width * avctx->height * 3 / 2 + 16) {
51 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
52 return AVERROR_INVALIDDATA;
53 }
54
55 pic->reference = 0;
56 if ((ret = ff_get_buffer(avctx, pic)) < 0)
57 return ret;
58
59 pic->pict_type = AV_PICTURE_TYPE_I;
60 pic->key_frame = 1;
61
62 if (AV_RL32(src) != 0x01000002) {
63 av_log_ask_for_sample(avctx, "Unknown frame header %X\n", AV_RL32(src));
64 return AVERROR_PATCHWELCOME;
65 }
66 src += 16;
67
68 Y1 = pic->data[0];
69 Y2 = pic->data[0] + pic->linesize[0];
70 U = pic->data[1];
71 V = pic->data[2];
72 for (h = 0; h < avctx->height; h += 2) {
73 for (w = 0; w < avctx->width; w += 2) {
74 AV_COPY16(Y1 + w, src);
75 AV_COPY16(Y2 + w, src + 2);
76 U[w >> 1] = src[4] + 0x80;
77 V[w >> 1] = src[5] + 0x80;
78 src += 6;
79 }
80 Y1 += pic->linesize[0] << 1;
81 Y2 += pic->linesize[0] << 1;
82 U += pic->linesize[1];
83 V += pic->linesize[2];
84 }
85
86 *got_frame = 1;
87 *(AVFrame*)data = *pic;
88
89 return avpkt->size;
90 }
91
92 static av_cold int decode_close(AVCodecContext *avctx)
93 {
94 AVFrame *pic = avctx->coded_frame;
95 if (pic->data[0])
96 avctx->release_buffer(avctx, pic);
97 av_freep(&avctx->coded_frame);
98
99 return 0;
100 }
101
102 AVCodec ff_dxtory_decoder = {
103 .name = "dxtory",
104 .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
105 .type = AVMEDIA_TYPE_VIDEO,
106 .id = AV_CODEC_ID_DXTORY,
107 .init = decode_init,
108 .close = decode_close,
109 .decode = decode_frame,
110 .capabilities = CODEC_CAP_DR1,
111 };