]> git.sesse.net Git - ffmpeg/blob - libavcodec/msvideo1.c
dxva2: Keep code shared between dxva2 and d3d11va under the correct #if
[ffmpeg] / libavcodec / msvideo1.c
1 /*
2  * Microsoft Video-1 Decoder
3  * Copyright (C) 2003 The FFmpeg project
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  * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
25  * For more information about the MS Video-1 format, visit:
26  *   http://www.pcisys.net/~melanson/codecs/
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
35 #include "avcodec.h"
36 #include "internal.h"
37
38 #define PALETTE_COUNT 256
39 #define CHECK_STREAM_PTR(n) \
40   if ((stream_ptr + n) > s->size ) { \
41     av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
42       stream_ptr + n, s->size); \
43     return; \
44   }
45
46 typedef struct Msvideo1Context {
47
48     AVCodecContext *avctx;
49     AVFrame *frame;
50
51     const unsigned char *buf;
52     int size;
53
54     int mode_8bit;  /* if it's not 8-bit, it's 16-bit */
55
56     uint32_t pal[256];
57 } Msvideo1Context;
58
59 static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
60 {
61     Msvideo1Context *s = avctx->priv_data;
62
63     s->avctx = avctx;
64
65     /* figure out the colorspace based on the presence of a palette */
66     if (s->avctx->bits_per_coded_sample == 8) {
67         s->mode_8bit = 1;
68         avctx->pix_fmt = AV_PIX_FMT_PAL8;
69     } else {
70         s->mode_8bit = 0;
71         avctx->pix_fmt = AV_PIX_FMT_RGB555;
72     }
73
74     s->frame = av_frame_alloc();
75     if (!s->frame)
76         return AVERROR(ENOMEM);
77
78     return 0;
79 }
80
81 static void msvideo1_decode_8bit(Msvideo1Context *s)
82 {
83     int block_ptr, pixel_ptr;
84     int total_blocks;
85     int pixel_x, pixel_y;  /* pixel width and height iterators */
86     int block_x, block_y;  /* block width and height iterators */
87     int blocks_wide, blocks_high;  /* width and height in 4x4 blocks */
88     int block_inc;
89     int row_dec;
90
91     /* decoding parameters */
92     int stream_ptr;
93     unsigned char byte_a, byte_b;
94     unsigned short flags;
95     int skip_blocks;
96     unsigned char colors[8];
97     unsigned char *pixels = s->frame->data[0];
98     int stride = s->frame->linesize[0];
99
100     stream_ptr = 0;
101     skip_blocks = 0;
102     blocks_wide = s->avctx->width / 4;
103     blocks_high = s->avctx->height / 4;
104     total_blocks = blocks_wide * blocks_high;
105     block_inc = 4;
106     row_dec = stride + 4;
107
108     for (block_y = blocks_high; block_y > 0; block_y--) {
109         block_ptr = ((block_y * 4) - 1) * stride;
110         for (block_x = blocks_wide; block_x > 0; block_x--) {
111             /* check if this block should be skipped */
112             if (skip_blocks) {
113                 block_ptr += block_inc;
114                 skip_blocks--;
115                 total_blocks--;
116                 continue;
117             }
118
119             pixel_ptr = block_ptr;
120
121             /* get the next two bytes in the encoded data stream */
122             CHECK_STREAM_PTR(2);
123             byte_a = s->buf[stream_ptr++];
124             byte_b = s->buf[stream_ptr++];
125
126             /* check if the decode is finished */
127             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
128                 return;
129             else if ((byte_b & 0xFC) == 0x84) {
130                 /* skip code, but don't count the current block */
131                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
132             } else if (byte_b < 0x80) {
133                 /* 2-color encoding */
134                 flags = (byte_b << 8) | byte_a;
135
136                 CHECK_STREAM_PTR(2);
137                 colors[0] = s->buf[stream_ptr++];
138                 colors[1] = s->buf[stream_ptr++];
139
140                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
141                     for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
142                         pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
143                     pixel_ptr -= row_dec;
144                 }
145             } else if (byte_b >= 0x90) {
146                 /* 8-color encoding */
147                 flags = (byte_b << 8) | byte_a;
148
149                 CHECK_STREAM_PTR(8);
150                 memcpy(colors, &s->buf[stream_ptr], 8);
151                 stream_ptr += 8;
152
153                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
154                     for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
155                         pixels[pixel_ptr++] =
156                             colors[((pixel_y & 0x2) << 1) +
157                                 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
158                     pixel_ptr -= row_dec;
159                 }
160             } else {
161                 /* 1-color encoding */
162                 colors[0] = byte_a;
163
164                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
165                     for (pixel_x = 0; pixel_x < 4; pixel_x++)
166                         pixels[pixel_ptr++] = colors[0];
167                     pixel_ptr -= row_dec;
168                 }
169             }
170
171             block_ptr += block_inc;
172             total_blocks--;
173         }
174     }
175
176     /* make the palette available on the way out */
177     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
178         memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
179 }
180
181 static void msvideo1_decode_16bit(Msvideo1Context *s)
182 {
183     int block_ptr, pixel_ptr;
184     int total_blocks;
185     int pixel_x, pixel_y;  /* pixel width and height iterators */
186     int block_x, block_y;  /* block width and height iterators */
187     int blocks_wide, blocks_high;  /* width and height in 4x4 blocks */
188     int block_inc;
189     int row_dec;
190
191     /* decoding parameters */
192     int stream_ptr;
193     unsigned char byte_a, byte_b;
194     unsigned short flags;
195     int skip_blocks;
196     unsigned short colors[8];
197     unsigned short *pixels = (unsigned short *)s->frame->data[0];
198     int stride = s->frame->linesize[0] / 2;
199
200     stream_ptr = 0;
201     skip_blocks = 0;
202     blocks_wide = s->avctx->width / 4;
203     blocks_high = s->avctx->height / 4;
204     total_blocks = blocks_wide * blocks_high;
205     block_inc = 4;
206     row_dec = stride + 4;
207
208     for (block_y = blocks_high; block_y > 0; block_y--) {
209         block_ptr = ((block_y * 4) - 1) * stride;
210         for (block_x = blocks_wide; block_x > 0; block_x--) {
211             /* check if this block should be skipped */
212             if (skip_blocks) {
213                 block_ptr += block_inc;
214                 skip_blocks--;
215                 total_blocks--;
216                 continue;
217             }
218
219             pixel_ptr = block_ptr;
220
221             /* get the next two bytes in the encoded data stream */
222             CHECK_STREAM_PTR(2);
223             byte_a = s->buf[stream_ptr++];
224             byte_b = s->buf[stream_ptr++];
225
226             /* check if the decode is finished */
227             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
228                 return;
229             } else if ((byte_b & 0xFC) == 0x84) {
230                 /* skip code, but don't count the current block */
231                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
232             } else if (byte_b < 0x80) {
233                 /* 2- or 8-color encoding modes */
234                 flags = (byte_b << 8) | byte_a;
235
236                 CHECK_STREAM_PTR(4);
237                 colors[0] = AV_RL16(&s->buf[stream_ptr]);
238                 stream_ptr += 2;
239                 colors[1] = AV_RL16(&s->buf[stream_ptr]);
240                 stream_ptr += 2;
241
242                 if (colors[0] & 0x8000) {
243                     /* 8-color encoding */
244                     CHECK_STREAM_PTR(12);
245                     colors[2] = AV_RL16(&s->buf[stream_ptr]);
246                     stream_ptr += 2;
247                     colors[3] = AV_RL16(&s->buf[stream_ptr]);
248                     stream_ptr += 2;
249                     colors[4] = AV_RL16(&s->buf[stream_ptr]);
250                     stream_ptr += 2;
251                     colors[5] = AV_RL16(&s->buf[stream_ptr]);
252                     stream_ptr += 2;
253                     colors[6] = AV_RL16(&s->buf[stream_ptr]);
254                     stream_ptr += 2;
255                     colors[7] = AV_RL16(&s->buf[stream_ptr]);
256                     stream_ptr += 2;
257
258                     for (pixel_y = 0; pixel_y < 4; pixel_y++) {
259                         for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
260                             pixels[pixel_ptr++] =
261                                 colors[((pixel_y & 0x2) << 1) +
262                                     (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
263                         pixel_ptr -= row_dec;
264                     }
265                 } else {
266                     /* 2-color encoding */
267                     for (pixel_y = 0; pixel_y < 4; pixel_y++) {
268                         for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
269                             pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
270                         pixel_ptr -= row_dec;
271                     }
272                 }
273             } else {
274                 /* otherwise, it's a 1-color block */
275                 colors[0] = (byte_b << 8) | byte_a;
276
277                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
278                     for (pixel_x = 0; pixel_x < 4; pixel_x++)
279                         pixels[pixel_ptr++] = colors[0];
280                     pixel_ptr -= row_dec;
281                 }
282             }
283
284             block_ptr += block_inc;
285             total_blocks--;
286         }
287     }
288 }
289
290 static int msvideo1_decode_frame(AVCodecContext *avctx,
291                                 void *data, int *got_frame,
292                                 AVPacket *avpkt)
293 {
294     const uint8_t *buf = avpkt->data;
295     int buf_size = avpkt->size;
296     Msvideo1Context *s = avctx->priv_data;
297     int ret;
298
299     s->buf = buf;
300     s->size = buf_size;
301
302     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
303         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
304         return ret;
305     }
306
307     if (s->mode_8bit) {
308         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
309
310         if (pal) {
311             memcpy(s->pal, pal, AVPALETTE_SIZE);
312             s->frame->palette_has_changed = 1;
313         }
314     }
315
316     if (s->mode_8bit)
317         msvideo1_decode_8bit(s);
318     else
319         msvideo1_decode_16bit(s);
320
321     if ((ret = av_frame_ref(data, s->frame)) < 0)
322         return ret;
323
324     *got_frame      = 1;
325
326     /* report that the buffer was completely consumed */
327     return buf_size;
328 }
329
330 static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
331 {
332     Msvideo1Context *s = avctx->priv_data;
333
334     av_frame_free(&s->frame);
335
336     return 0;
337 }
338
339 AVCodec ff_msvideo1_decoder = {
340     .name           = "msvideo1",
341     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
342     .type           = AVMEDIA_TYPE_VIDEO,
343     .id             = AV_CODEC_ID_MSVIDEO1,
344     .priv_data_size = sizeof(Msvideo1Context),
345     .init           = msvideo1_decode_init,
346     .close          = msvideo1_decode_end,
347     .decode         = msvideo1_decode_frame,
348     .capabilities   = AV_CODEC_CAP_DR1,
349 };