OSDN Git Service

doc: explain __STDC_CONSTANT_MACROS in C++
[coroid/libav_saccubus.git] / libavcodec / smc.c
1 /*
2  * Quicktime Graphics (SMC) Video 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  * QT SMC Video Decoder by Mike Melanson (melanson@pcisys.net)
25  * For more information about the SMC format, visit:
26  *   http://www.pcisys.net/~melanson/codecs/
27  *
28  * The SMC decoder outputs PAL8 colorspace data.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "libavutil/intreadwrite.h"
36 #include "avcodec.h"
37
38 #define CPAIR 2
39 #define CQUAD 4
40 #define COCTET 8
41
42 #define COLORS_PER_TABLE 256
43
44 typedef struct SmcContext {
45
46     AVCodecContext *avctx;
47     AVFrame frame;
48
49     const unsigned char *buf;
50     int size;
51
52     /* SMC color tables */
53     unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
54     unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
55     unsigned char color_octets[COLORS_PER_TABLE * COCTET];
56
57     uint32_t pal[256];
58 } SmcContext;
59
60 #define GET_BLOCK_COUNT() \
61   (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
62
63 #define ADVANCE_BLOCK() \
64 { \
65     pixel_ptr += 4; \
66     if (pixel_ptr >= width) \
67     { \
68         pixel_ptr = 0; \
69         row_ptr += stride * 4; \
70     } \
71     total_blocks--; \
72     if (total_blocks < 0) \
73     { \
74         av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
75         return; \
76     } \
77 }
78
79 static void smc_decode_stream(SmcContext *s)
80 {
81     int width = s->avctx->width;
82     int height = s->avctx->height;
83     int stride = s->frame.linesize[0];
84     int i;
85     int stream_ptr = 0;
86     int chunk_size;
87     unsigned char opcode;
88     int n_blocks;
89     unsigned int color_flags;
90     unsigned int color_flags_a;
91     unsigned int color_flags_b;
92     unsigned int flag_mask;
93
94     unsigned char *pixels = s->frame.data[0];
95
96     int image_size = height * s->frame.linesize[0];
97     int row_ptr = 0;
98     int pixel_ptr = 0;
99     int pixel_x, pixel_y;
100     int row_inc = stride - 4;
101     int block_ptr;
102     int prev_block_ptr;
103     int prev_block_ptr1, prev_block_ptr2;
104     int prev_block_flag;
105     int total_blocks;
106     int color_table_index;  /* indexes to color pair, quad, or octet tables */
107     int pixel;
108
109     int color_pair_index = 0;
110     int color_quad_index = 0;
111     int color_octet_index = 0;
112
113     /* make the palette available */
114     memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
115
116     chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
117     stream_ptr += 4;
118     if (chunk_size != s->size)
119         av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
120             chunk_size, s->size);
121
122     chunk_size = s->size;
123     total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
124
125     /* traverse through the blocks */
126     while (total_blocks) {
127         /* sanity checks */
128         /* make sure stream ptr hasn't gone out of bounds */
129         if (stream_ptr > chunk_size) {
130             av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
131                 stream_ptr, chunk_size);
132             return;
133         }
134         /* make sure the row pointer hasn't gone wild */
135         if (row_ptr >= image_size) {
136             av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
137                 row_ptr, image_size);
138             return;
139         }
140
141         opcode = s->buf[stream_ptr++];
142         switch (opcode & 0xF0) {
143         /* skip n blocks */
144         case 0x00:
145         case 0x10:
146             n_blocks = GET_BLOCK_COUNT();
147             while (n_blocks--) {
148                 ADVANCE_BLOCK();
149             }
150             break;
151
152         /* repeat last block n times */
153         case 0x20:
154         case 0x30:
155             n_blocks = GET_BLOCK_COUNT();
156
157             /* sanity check */
158             if ((row_ptr == 0) && (pixel_ptr == 0)) {
159                 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
160                     opcode & 0xF0);
161                 break;
162             }
163
164             /* figure out where the previous block started */
165             if (pixel_ptr == 0)
166                 prev_block_ptr1 =
167                     (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
168             else
169                 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
170
171             while (n_blocks--) {
172                 block_ptr = row_ptr + pixel_ptr;
173                 prev_block_ptr = prev_block_ptr1;
174                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
175                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
176                         pixels[block_ptr++] = pixels[prev_block_ptr++];
177                     }
178                     block_ptr += row_inc;
179                     prev_block_ptr += row_inc;
180                 }
181                 ADVANCE_BLOCK();
182             }
183             break;
184
185         /* repeat previous pair of blocks n times */
186         case 0x40:
187         case 0x50:
188             n_blocks = GET_BLOCK_COUNT();
189             n_blocks *= 2;
190
191             /* sanity check */
192             if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
193                 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
194                     opcode & 0xF0);
195                 break;
196             }
197
198             /* figure out where the previous 2 blocks started */
199             if (pixel_ptr == 0)
200                 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
201                     s->avctx->width - 4 * 2;
202             else if (pixel_ptr == 4)
203                 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
204             else
205                 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
206
207             if (pixel_ptr == 0)
208                 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
209             else
210                 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
211
212             prev_block_flag = 0;
213             while (n_blocks--) {
214                 block_ptr = row_ptr + pixel_ptr;
215                 if (prev_block_flag)
216                     prev_block_ptr = prev_block_ptr2;
217                 else
218                     prev_block_ptr = prev_block_ptr1;
219                 prev_block_flag = !prev_block_flag;
220
221                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
222                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
223                         pixels[block_ptr++] = pixels[prev_block_ptr++];
224                     }
225                     block_ptr += row_inc;
226                     prev_block_ptr += row_inc;
227                 }
228                 ADVANCE_BLOCK();
229             }
230             break;
231
232         /* 1-color block encoding */
233         case 0x60:
234         case 0x70:
235             n_blocks = GET_BLOCK_COUNT();
236             pixel = s->buf[stream_ptr++];
237
238             while (n_blocks--) {
239                 block_ptr = row_ptr + pixel_ptr;
240                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
241                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
242                         pixels[block_ptr++] = pixel;
243                     }
244                     block_ptr += row_inc;
245                 }
246                 ADVANCE_BLOCK();
247             }
248             break;
249
250         /* 2-color block encoding */
251         case 0x80:
252         case 0x90:
253             n_blocks = (opcode & 0x0F) + 1;
254
255             /* figure out which color pair to use to paint the 2-color block */
256             if ((opcode & 0xF0) == 0x80) {
257                 /* fetch the next 2 colors from bytestream and store in next
258                  * available entry in the color pair table */
259                 for (i = 0; i < CPAIR; i++) {
260                     pixel = s->buf[stream_ptr++];
261                     color_table_index = CPAIR * color_pair_index + i;
262                     s->color_pairs[color_table_index] = pixel;
263                 }
264                 /* this is the base index to use for this block */
265                 color_table_index = CPAIR * color_pair_index;
266                 color_pair_index++;
267                 /* wraparound */
268                 if (color_pair_index == COLORS_PER_TABLE)
269                     color_pair_index = 0;
270             } else
271                 color_table_index = CPAIR * s->buf[stream_ptr++];
272
273             while (n_blocks--) {
274                 color_flags = AV_RB16(&s->buf[stream_ptr]);
275                 stream_ptr += 2;
276                 flag_mask = 0x8000;
277                 block_ptr = row_ptr + pixel_ptr;
278                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
279                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
280                         if (color_flags & flag_mask)
281                             pixel = color_table_index + 1;
282                         else
283                             pixel = color_table_index;
284                         flag_mask >>= 1;
285                         pixels[block_ptr++] = s->color_pairs[pixel];
286                     }
287                     block_ptr += row_inc;
288                 }
289                 ADVANCE_BLOCK();
290             }
291             break;
292
293         /* 4-color block encoding */
294         case 0xA0:
295         case 0xB0:
296             n_blocks = (opcode & 0x0F) + 1;
297
298             /* figure out which color quad to use to paint the 4-color block */
299             if ((opcode & 0xF0) == 0xA0) {
300                 /* fetch the next 4 colors from bytestream and store in next
301                  * available entry in the color quad table */
302                 for (i = 0; i < CQUAD; i++) {
303                     pixel = s->buf[stream_ptr++];
304                     color_table_index = CQUAD * color_quad_index + i;
305                     s->color_quads[color_table_index] = pixel;
306                 }
307                 /* this is the base index to use for this block */
308                 color_table_index = CQUAD * color_quad_index;
309                 color_quad_index++;
310                 /* wraparound */
311                 if (color_quad_index == COLORS_PER_TABLE)
312                     color_quad_index = 0;
313             } else
314                 color_table_index = CQUAD * s->buf[stream_ptr++];
315
316             while (n_blocks--) {
317                 color_flags = AV_RB32(&s->buf[stream_ptr]);
318                 stream_ptr += 4;
319                 /* flag mask actually acts as a bit shift count here */
320                 flag_mask = 30;
321                 block_ptr = row_ptr + pixel_ptr;
322                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
323                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
324                         pixel = color_table_index +
325                             ((color_flags >> flag_mask) & 0x03);
326                         flag_mask -= 2;
327                         pixels[block_ptr++] = s->color_quads[pixel];
328                     }
329                     block_ptr += row_inc;
330                 }
331                 ADVANCE_BLOCK();
332             }
333             break;
334
335         /* 8-color block encoding */
336         case 0xC0:
337         case 0xD0:
338             n_blocks = (opcode & 0x0F) + 1;
339
340             /* figure out which color octet to use to paint the 8-color block */
341             if ((opcode & 0xF0) == 0xC0) {
342                 /* fetch the next 8 colors from bytestream and store in next
343                  * available entry in the color octet table */
344                 for (i = 0; i < COCTET; i++) {
345                     pixel = s->buf[stream_ptr++];
346                     color_table_index = COCTET * color_octet_index + i;
347                     s->color_octets[color_table_index] = pixel;
348                 }
349                 /* this is the base index to use for this block */
350                 color_table_index = COCTET * color_octet_index;
351                 color_octet_index++;
352                 /* wraparound */
353                 if (color_octet_index == COLORS_PER_TABLE)
354                     color_octet_index = 0;
355             } else
356                 color_table_index = COCTET * s->buf[stream_ptr++];
357
358             while (n_blocks--) {
359                 /*
360                   For this input of 6 hex bytes:
361                     01 23 45 67 89 AB
362                   Mangle it to this output:
363                     flags_a = xx012456, flags_b = xx89A37B
364                 */
365                 /* build the color flags */
366                 color_flags_a =
367                     ((AV_RB16(s->buf + stream_ptr    ) & 0xFFF0) << 8) |
368                      (AV_RB16(s->buf + stream_ptr + 2) >> 4);
369                 color_flags_b =
370                     ((AV_RB16(s->buf + stream_ptr + 4) & 0xFFF0) << 8) |
371                     ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
372                     ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
373                     (s->buf[stream_ptr + 5] & 0x0F);
374                 stream_ptr += 6;
375
376                 color_flags = color_flags_a;
377                 /* flag mask actually acts as a bit shift count here */
378                 flag_mask = 21;
379                 block_ptr = row_ptr + pixel_ptr;
380                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
381                     /* reload flags at third row (iteration pixel_y == 2) */
382                     if (pixel_y == 2) {
383                         color_flags = color_flags_b;
384                         flag_mask = 21;
385                     }
386                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
387                         pixel = color_table_index +
388                             ((color_flags >> flag_mask) & 0x07);
389                         flag_mask -= 3;
390                         pixels[block_ptr++] = s->color_octets[pixel];
391                     }
392                     block_ptr += row_inc;
393                 }
394                 ADVANCE_BLOCK();
395             }
396             break;
397
398         /* 16-color block encoding (every pixel is a different color) */
399         case 0xE0:
400             n_blocks = (opcode & 0x0F) + 1;
401
402             while (n_blocks--) {
403                 block_ptr = row_ptr + pixel_ptr;
404                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
405                     for (pixel_x = 0; pixel_x < 4; pixel_x++) {
406                         pixels[block_ptr++] = s->buf[stream_ptr++];
407                     }
408                     block_ptr += row_inc;
409                 }
410                 ADVANCE_BLOCK();
411             }
412             break;
413
414         case 0xF0:
415             av_log(s->avctx, AV_LOG_INFO, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
416             break;
417         }
418     }
419 }
420
421 static av_cold int smc_decode_init(AVCodecContext *avctx)
422 {
423     SmcContext *s = avctx->priv_data;
424
425     s->avctx = avctx;
426     avctx->pix_fmt = PIX_FMT_PAL8;
427
428     s->frame.data[0] = NULL;
429
430     return 0;
431 }
432
433 static int smc_decode_frame(AVCodecContext *avctx,
434                              void *data, int *data_size,
435                              AVPacket *avpkt)
436 {
437     const uint8_t *buf = avpkt->data;
438     int buf_size = avpkt->size;
439     SmcContext *s = avctx->priv_data;
440     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
441
442     s->buf = buf;
443     s->size = buf_size;
444
445     s->frame.reference = 1;
446     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
447                             FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
448     if (avctx->reget_buffer(avctx, &s->frame)) {
449         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
450         return -1;
451     }
452
453     if (pal) {
454         s->frame.palette_has_changed = 1;
455         memcpy(s->pal, pal, AVPALETTE_SIZE);
456     }
457
458     smc_decode_stream(s);
459
460     *data_size = sizeof(AVFrame);
461     *(AVFrame*)data = s->frame;
462
463     /* always report that the buffer was completely consumed */
464     return buf_size;
465 }
466
467 static av_cold int smc_decode_end(AVCodecContext *avctx)
468 {
469     SmcContext *s = avctx->priv_data;
470
471     if (s->frame.data[0])
472         avctx->release_buffer(avctx, &s->frame);
473
474     return 0;
475 }
476
477 AVCodec ff_smc_decoder = {
478     .name           = "smc",
479     .type           = AVMEDIA_TYPE_VIDEO,
480     .id             = CODEC_ID_SMC,
481     .priv_data_size = sizeof(SmcContext),
482     .init           = smc_decode_init,
483     .close          = smc_decode_end,
484     .decode         = smc_decode_frame,
485     .capabilities   = CODEC_CAP_DR1,
486     .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),
487 };