]> git.sesse.net Git - ffmpeg/blob - libavcodec/jvdec.c
lavc: Add option to encode MPEG-2 AAC with libfdk-aac
[ffmpeg] / libavcodec / jvdec.c
1 /*
2  * Bitmap Brothers JV video decoder
3  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
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  * Bitmap Brothers JV video decoder
25  * @author Peter Ross <pross@xvid.org>
26  */
27
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "get_bits.h"
31 #include "internal.h"
32 #include "libavutil/intreadwrite.h"
33
34 typedef struct JvContext {
35     DSPContext dsp;
36     AVFrame    frame;
37     uint32_t   palette[AVPALETTE_COUNT];
38     int        palette_has_changed;
39 } JvContext;
40
41 static av_cold int decode_init(AVCodecContext *avctx)
42 {
43     JvContext *s = avctx->priv_data;
44     avctx->pix_fmt = AV_PIX_FMT_PAL8;
45     ff_dsputil_init(&s->dsp, avctx);
46     return 0;
47 }
48
49 /**
50  * Decode 2x2 block
51  */
52 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
53 {
54     int i, j, v[2];
55
56     switch (get_bits(gb, 2)) {
57     case 1:
58         v[0] = get_bits(gb, 8);
59         for (j = 0; j < 2; j++)
60             memset(dst + j*linesize, v[0], 2);
61         break;
62     case 2:
63         v[0] = get_bits(gb, 8);
64         v[1] = get_bits(gb, 8);
65         for (j = 0; j < 2; j++)
66             for (i = 0; i < 2; i++)
67                 dst[j*linesize + i] = v[get_bits1(gb)];
68         break;
69     case 3:
70         for (j = 0; j < 2; j++)
71             for (i = 0; i < 2; i++)
72                 dst[j*linesize + i] = get_bits(gb, 8);
73     }
74 }
75
76 /**
77  * Decode 4x4 block
78  */
79 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
80 {
81     int i, j, v[2];
82
83     switch (get_bits(gb, 2)) {
84     case 1:
85         v[0] = get_bits(gb, 8);
86         for (j = 0; j < 4; j++)
87             memset(dst + j*linesize, v[0], 4);
88         break;
89     case 2:
90         v[0] = get_bits(gb, 8);
91         v[1] = get_bits(gb, 8);
92         for (j = 2; j >= 0; j -= 2) {
93             for (i = 0; i < 4; i++)
94                 dst[j*linesize + i]     = v[get_bits1(gb)];
95             for (i = 0; i < 4; i++)
96                 dst[(j+1)*linesize + i] = v[get_bits1(gb)];
97         }
98         break;
99     case 3:
100         for (j = 0; j < 4; j += 2)
101             for (i = 0; i < 4; i += 2)
102                 decode2x2(gb, dst + j*linesize + i, linesize);
103     }
104 }
105
106 /**
107  * Decode 8x8 block
108  */
109 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
110 {
111     int i, j, v[2];
112
113     switch (get_bits(gb, 2)) {
114     case 1:
115         v[0] = get_bits(gb, 8);
116         dsp->fill_block_tab[1](dst, v[0], linesize, 8);
117         break;
118     case 2:
119         v[0] = get_bits(gb, 8);
120         v[1] = get_bits(gb, 8);
121         for (j = 7; j >= 0; j--)
122             for (i = 0; i <  8; i++)
123                 dst[j*linesize + i] = v[get_bits1(gb)];
124         break;
125     case 3:
126         for (j = 0; j < 8; j += 4)
127             for (i = 0; i < 8; i += 4)
128                 decode4x4(gb, dst + j*linesize + i, linesize);
129     }
130 }
131
132 static int decode_frame(AVCodecContext *avctx,
133                         void *data, int *got_frame,
134                         AVPacket *avpkt)
135 {
136     JvContext *s           = avctx->priv_data;
137     int buf_size           = avpkt->size;
138     const uint8_t *buf     = avpkt->data;
139     const uint8_t *buf_end = buf + buf_size;
140     int video_size, video_type, i, j, ret;
141
142     video_size = AV_RL32(buf);
143     video_type = buf[4];
144     buf += 5;
145
146     if (video_size) {
147         if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0) {
148             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
149             return ret;
150         }
151
152         if (video_type == 0 || video_type == 1) {
153             GetBitContext gb;
154             init_get_bits(&gb, buf, 8 * FFMIN(video_size, buf_end - buf));
155
156             for (j = 0; j < avctx->height; j += 8)
157                 for (i = 0; i < avctx->width; i += 8)
158                     decode8x8(&gb, s->frame.data[0] + j*s->frame.linesize[0] + i,
159                               s->frame.linesize[0], &s->dsp);
160
161             buf += video_size;
162         } else if (video_type == 2) {
163             if (buf + 1 <= buf_end) {
164                 int v = *buf++;
165                 for (j = 0; j < avctx->height; j++)
166                     memset(s->frame.data[0] + j*s->frame.linesize[0], v, avctx->width);
167             }
168         } else {
169             av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
170             return AVERROR_INVALIDDATA;
171         }
172     }
173
174     if (buf < buf_end) {
175         for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
176             s->palette[i] = AV_RB24(buf) << 2;
177             buf += 3;
178         }
179         s->palette_has_changed = 1;
180     }
181
182     if (video_size) {
183         s->frame.key_frame           = 1;
184         s->frame.pict_type           = AV_PICTURE_TYPE_I;
185         s->frame.palette_has_changed = s->palette_has_changed;
186         s->palette_has_changed       = 0;
187         memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
188
189         if ((ret = av_frame_ref(data, &s->frame)) < 0)
190             return ret;
191         *got_frame = 1;
192     }
193
194     return buf_size;
195 }
196
197 static av_cold int decode_close(AVCodecContext *avctx)
198 {
199     JvContext *s = avctx->priv_data;
200
201     av_frame_unref(&s->frame);
202
203     return 0;
204 }
205
206 AVCodec ff_jv_decoder = {
207     .name           = "jv",
208     .long_name      = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
209     .type           = AVMEDIA_TYPE_VIDEO,
210     .id             = AV_CODEC_ID_JV,
211     .priv_data_size = sizeof(JvContext),
212     .init           = decode_init,
213     .close          = decode_close,
214     .decode         = decode_frame,
215     .capabilities   = CODEC_CAP_DR1,
216 };