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