1/*
2 * Miro VideoXL codec
3 * Copyright (c) 2004 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * Miro VideoXL codec.
25 */
26
27#include "libavutil/intreadwrite.h"
28#include "avcodec.h"
29
30typedef struct VideoXLContext{
31    AVCodecContext *avctx;
32    AVFrame pic;
33} VideoXLContext;
34
35static const int xl_table[32] = {
36   0,   1,   2,   3,   4,   5,   6,   7,
37   8,   9,  12,  15,  20,  25,  34,  46,
38  64,  82,  94, 103, 108, 113, 116, 119,
39 120, 121, 122, 123, 124, 125, 126, 127};
40
41static int decode_frame(AVCodecContext *avctx,
42                        void *data, int *data_size,
43                        AVPacket *avpkt)
44{
45    const uint8_t *buf = avpkt->data;
46    int buf_size = avpkt->size;
47    VideoXLContext * const a = avctx->priv_data;
48    AVFrame * const p= (AVFrame*)&a->pic;
49    uint8_t *Y, *U, *V;
50    int i, j;
51    int stride;
52    uint32_t val;
53    int y0, y1, y2, y3 = 0, c0 = 0, c1 = 0;
54
55    if(p->data[0])
56        avctx->release_buffer(avctx, p);
57
58    p->reference = 0;
59    if(avctx->get_buffer(avctx, p) < 0){
60        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
61        return -1;
62    }
63    p->pict_type= AV_PICTURE_TYPE_I;
64    p->key_frame= 1;
65
66    Y = a->pic.data[0];
67    U = a->pic.data[1];
68    V = a->pic.data[2];
69
70    stride = avctx->width - 4;
71
72    if (buf_size < avctx->width * avctx->height) {
73        av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
74        return AVERROR_INVALIDDATA;
75    }
76
77    for (i = 0; i < avctx->height; i++) {
78        /* lines are stored in reversed order */
79        buf += stride;
80
81        for (j = 0; j < avctx->width; j += 4) {
82            /* value is stored in LE dword with word swapped */
83            val = AV_RL32(buf);
84            buf -= 4;
85            val = ((val >> 16) & 0xFFFF) | ((val & 0xFFFF) << 16);
86
87            if(!j)
88                y0 = (val & 0x1F) << 2;
89            else
90                y0 = y3 + xl_table[val & 0x1F];
91            val >>= 5;
92            y1 = y0 + xl_table[val & 0x1F];
93            val >>= 5;
94            y2 = y1 + xl_table[val & 0x1F];
95            val >>= 6; /* align to word */
96            y3 = y2 + xl_table[val & 0x1F];
97            val >>= 5;
98            if(!j)
99                c0 = (val & 0x1F) << 2;
100            else
101                c0 += xl_table[val & 0x1F];
102            val >>= 5;
103            if(!j)
104                c1 = (val & 0x1F) << 2;
105            else
106                c1 += xl_table[val & 0x1F];
107
108            Y[j + 0] = y0 << 1;
109            Y[j + 1] = y1 << 1;
110            Y[j + 2] = y2 << 1;
111            Y[j + 3] = y3 << 1;
112
113            U[j >> 2] = c0 << 1;
114            V[j >> 2] = c1 << 1;
115        }
116
117        buf += avctx->width + 4;
118        Y += a->pic.linesize[0];
119        U += a->pic.linesize[1];
120        V += a->pic.linesize[2];
121    }
122
123    *data_size = sizeof(AVFrame);
124    *(AVFrame*)data = a->pic;
125
126    return buf_size;
127}
128
129static av_cold int decode_init(AVCodecContext *avctx){
130//    VideoXLContext * const a = avctx->priv_data;
131
132    avctx->pix_fmt= PIX_FMT_YUV411P;
133
134    return 0;
135}
136
137static av_cold int decode_end(AVCodecContext *avctx){
138    VideoXLContext * const a = avctx->priv_data;
139    AVFrame *pic = &a->pic;
140
141    if (pic->data[0])
142        avctx->release_buffer(avctx, pic);
143
144    return 0;
145}
146
147AVCodec ff_xl_decoder = {
148    .name           = "xl",
149    .type           = AVMEDIA_TYPE_VIDEO,
150    .id             = CODEC_ID_VIXL,
151    .priv_data_size = sizeof(VideoXLContext),
152    .init           = decode_init,
153    .close          = decode_end,
154    .decode         = decode_frame,
155    .capabilities   = CODEC_CAP_DR1,
156    .long_name = NULL_IF_CONFIG_SMALL("Miro VideoXL"),
157};
158