2 * Quicktime Graphics (SMC) Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavcodec/smc.c
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/
28 * The SMC decoder outputs PAL8 colorspace data.
36 #include "libavutil/intreadwrite.h"
43 #define COLORS_PER_TABLE 256
45 typedef struct SmcContext
{
47 AVCodecContext
*avctx
;
50 const unsigned char *buf
;
53 /* SMC color tables */
54 unsigned char color_pairs
[COLORS_PER_TABLE
* CPAIR
];
55 unsigned char color_quads
[COLORS_PER_TABLE
* CQUAD
];
56 unsigned char color_octets
[COLORS_PER_TABLE
* COCTET
];
60 #define GET_BLOCK_COUNT() \
61 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
63 #define ADVANCE_BLOCK() \
66 if (pixel_ptr >= width) \
69 row_ptr += stride * 4; \
72 if (total_blocks < 0) \
74 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
79 static void smc_decode_stream(SmcContext
*s
)
81 int width
= s
->avctx
->width
;
82 int height
= s
->avctx
->height
;
83 int stride
= s
->frame
.linesize
[0];
89 unsigned int color_flags
;
90 unsigned int color_flags_a
;
91 unsigned int color_flags_b
;
92 unsigned int flag_mask
;
94 unsigned char *pixels
= s
->frame
.data
[0];
96 int image_size
= height
* s
->frame
.linesize
[0];
100 int row_inc
= stride
- 4;
103 int prev_block_ptr1
, prev_block_ptr2
;
106 int color_table_index
; /* indexes to color pair, quad, or octet tables */
109 int color_pair_index
= 0;
110 int color_quad_index
= 0;
111 int color_octet_index
= 0;
113 /* make the palette available */
114 memcpy(s
->frame
.data
[1], s
->avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
115 if (s
->avctx
->palctrl
->palette_changed
) {
116 s
->frame
.palette_has_changed
= 1;
117 s
->avctx
->palctrl
->palette_changed
= 0;
120 chunk_size
= AV_RB32(&s
->buf
[stream_ptr
]) & 0x00FFFFFF;
122 if (chunk_size
!= s
->size
)
123 av_log(s
->avctx
, AV_LOG_INFO
, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
124 chunk_size
, s
->size
);
126 chunk_size
= s
->size
;
127 total_blocks
= ((s
->avctx
->width
+ 3) / 4) * ((s
->avctx
->height
+ 3) / 4);
129 /* traverse through the blocks */
130 while (total_blocks
) {
132 /* make sure stream ptr hasn't gone out of bounds */
133 if (stream_ptr
> chunk_size
) {
134 av_log(s
->avctx
, AV_LOG_INFO
, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
135 stream_ptr
, chunk_size
);
138 /* make sure the row pointer hasn't gone wild */
139 if (row_ptr
>= image_size
) {
140 av_log(s
->avctx
, AV_LOG_INFO
, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
141 row_ptr
, image_size
);
145 opcode
= s
->buf
[stream_ptr
++];
146 switch (opcode
& 0xF0) {
150 n_blocks
= GET_BLOCK_COUNT();
156 /* repeat last block n times */
159 n_blocks
= GET_BLOCK_COUNT();
162 if ((row_ptr
== 0) && (pixel_ptr
== 0)) {
163 av_log(s
->avctx
, AV_LOG_INFO
, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
168 /* figure out where the previous block started */
171 (row_ptr
- s
->avctx
->width
* 4) + s
->avctx
->width
- 4;
173 prev_block_ptr1
= row_ptr
+ pixel_ptr
- 4;
176 block_ptr
= row_ptr
+ pixel_ptr
;
177 prev_block_ptr
= prev_block_ptr1
;
178 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
179 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
180 pixels
[block_ptr
++] = pixels
[prev_block_ptr
++];
182 block_ptr
+= row_inc
;
183 prev_block_ptr
+= row_inc
;
189 /* repeat previous pair of blocks n times */
192 n_blocks
= GET_BLOCK_COUNT();
196 if ((row_ptr
== 0) && (pixel_ptr
< 2 * 4)) {
197 av_log(s
->avctx
, AV_LOG_INFO
, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
202 /* figure out where the previous 2 blocks started */
204 prev_block_ptr1
= (row_ptr
- s
->avctx
->width
* 4) +
205 s
->avctx
->width
- 4 * 2;
206 else if (pixel_ptr
== 4)
207 prev_block_ptr1
= (row_ptr
- s
->avctx
->width
* 4) + row_inc
;
209 prev_block_ptr1
= row_ptr
+ pixel_ptr
- 4 * 2;
212 prev_block_ptr2
= (row_ptr
- s
->avctx
->width
* 4) + row_inc
;
214 prev_block_ptr2
= row_ptr
+ pixel_ptr
- 4;
218 block_ptr
= row_ptr
+ pixel_ptr
;
220 prev_block_ptr
= prev_block_ptr2
;
222 prev_block_ptr
= prev_block_ptr1
;
223 prev_block_flag
= !prev_block_flag
;
225 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
226 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
227 pixels
[block_ptr
++] = pixels
[prev_block_ptr
++];
229 block_ptr
+= row_inc
;
230 prev_block_ptr
+= row_inc
;
236 /* 1-color block encoding */
239 n_blocks
= GET_BLOCK_COUNT();
240 pixel
= s
->buf
[stream_ptr
++];
243 block_ptr
= row_ptr
+ pixel_ptr
;
244 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
245 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
246 pixels
[block_ptr
++] = pixel
;
248 block_ptr
+= row_inc
;
254 /* 2-color block encoding */
257 n_blocks
= (opcode
& 0x0F) + 1;
259 /* figure out which color pair to use to paint the 2-color block */
260 if ((opcode
& 0xF0) == 0x80) {
261 /* fetch the next 2 colors from bytestream and store in next
262 * available entry in the color pair table */
263 for (i
= 0; i
< CPAIR
; i
++) {
264 pixel
= s
->buf
[stream_ptr
++];
265 color_table_index
= CPAIR
* color_pair_index
+ i
;
266 s
->color_pairs
[color_table_index
] = pixel
;
268 /* this is the base index to use for this block */
269 color_table_index
= CPAIR
* color_pair_index
;
272 if (color_pair_index
== COLORS_PER_TABLE
)
273 color_pair_index
= 0;
275 color_table_index
= CPAIR
* s
->buf
[stream_ptr
++];
278 color_flags
= AV_RB16(&s
->buf
[stream_ptr
]);
281 block_ptr
= row_ptr
+ pixel_ptr
;
282 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
283 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
284 if (color_flags
& flag_mask
)
285 pixel
= color_table_index
+ 1;
287 pixel
= color_table_index
;
289 pixels
[block_ptr
++] = s
->color_pairs
[pixel
];
291 block_ptr
+= row_inc
;
297 /* 4-color block encoding */
300 n_blocks
= (opcode
& 0x0F) + 1;
302 /* figure out which color quad to use to paint the 4-color block */
303 if ((opcode
& 0xF0) == 0xA0) {
304 /* fetch the next 4 colors from bytestream and store in next
305 * available entry in the color quad table */
306 for (i
= 0; i
< CQUAD
; i
++) {
307 pixel
= s
->buf
[stream_ptr
++];
308 color_table_index
= CQUAD
* color_quad_index
+ i
;
309 s
->color_quads
[color_table_index
] = pixel
;
311 /* this is the base index to use for this block */
312 color_table_index
= CQUAD
* color_quad_index
;
315 if (color_quad_index
== COLORS_PER_TABLE
)
316 color_quad_index
= 0;
318 color_table_index
= CQUAD
* s
->buf
[stream_ptr
++];
321 color_flags
= AV_RB32(&s
->buf
[stream_ptr
]);
323 /* flag mask actually acts as a bit shift count here */
325 block_ptr
= row_ptr
+ pixel_ptr
;
326 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
327 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
328 pixel
= color_table_index
+
329 ((color_flags
>> flag_mask
) & 0x03);
331 pixels
[block_ptr
++] = s
->color_quads
[pixel
];
333 block_ptr
+= row_inc
;
339 /* 8-color block encoding */
342 n_blocks
= (opcode
& 0x0F) + 1;
344 /* figure out which color octet to use to paint the 8-color block */
345 if ((opcode
& 0xF0) == 0xC0) {
346 /* fetch the next 8 colors from bytestream and store in next
347 * available entry in the color octet table */
348 for (i
= 0; i
< COCTET
; i
++) {
349 pixel
= s
->buf
[stream_ptr
++];
350 color_table_index
= COCTET
* color_octet_index
+ i
;
351 s
->color_octets
[color_table_index
] = pixel
;
353 /* this is the base index to use for this block */
354 color_table_index
= COCTET
* color_octet_index
;
357 if (color_octet_index
== COLORS_PER_TABLE
)
358 color_octet_index
= 0;
360 color_table_index
= COCTET
* s
->buf
[stream_ptr
++];
364 For this input of 6 hex bytes:
366 Mangle it to this output:
367 flags_a = xx012456, flags_b = xx89A37B
369 /* build the color flags */
370 color_flags_a
= color_flags_b
= 0;
372 (s
->buf
[stream_ptr
+ 0] << 16) |
373 ((s
->buf
[stream_ptr
+ 1] & 0xF0) << 8) |
374 ((s
->buf
[stream_ptr
+ 2] & 0xF0) << 4) |
375 ((s
->buf
[stream_ptr
+ 2] & 0x0F) << 4) |
376 ((s
->buf
[stream_ptr
+ 3] & 0xF0) >> 4);
378 (s
->buf
[stream_ptr
+ 4] << 16) |
379 ((s
->buf
[stream_ptr
+ 5] & 0xF0) << 8) |
380 ((s
->buf
[stream_ptr
+ 1] & 0x0F) << 8) |
381 ((s
->buf
[stream_ptr
+ 3] & 0x0F) << 4) |
382 (s
->buf
[stream_ptr
+ 5] & 0x0F);
385 color_flags
= color_flags_a
;
386 /* flag mask actually acts as a bit shift count here */
388 block_ptr
= row_ptr
+ pixel_ptr
;
389 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
390 /* reload flags at third row (iteration pixel_y == 2) */
392 color_flags
= color_flags_b
;
395 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
396 pixel
= color_table_index
+
397 ((color_flags
>> flag_mask
) & 0x07);
399 pixels
[block_ptr
++] = s
->color_octets
[pixel
];
401 block_ptr
+= row_inc
;
407 /* 16-color block encoding (every pixel is a different color) */
409 n_blocks
= (opcode
& 0x0F) + 1;
412 block_ptr
= row_ptr
+ pixel_ptr
;
413 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
414 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++) {
415 pixels
[block_ptr
++] = s
->buf
[stream_ptr
++];
417 block_ptr
+= row_inc
;
424 av_log(s
->avctx
, AV_LOG_INFO
, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
430 static av_cold
int smc_decode_init(AVCodecContext
*avctx
)
432 SmcContext
*s
= avctx
->priv_data
;
435 avctx
->pix_fmt
= PIX_FMT_PAL8
;
437 s
->frame
.data
[0] = NULL
;
442 static int smc_decode_frame(AVCodecContext
*avctx
,
443 void *data
, int *data_size
,
446 const uint8_t *buf
= avpkt
->data
;
447 int buf_size
= avpkt
->size
;
448 SmcContext
*s
= avctx
->priv_data
;
453 s
->frame
.reference
= 1;
454 s
->frame
.buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
|
455 FF_BUFFER_HINTS_REUSABLE
| FF_BUFFER_HINTS_READABLE
;
456 if (avctx
->reget_buffer(avctx
, &s
->frame
)) {
457 av_log(s
->avctx
, AV_LOG_ERROR
, "reget_buffer() failed\n");
461 smc_decode_stream(s
);
463 *data_size
= sizeof(AVFrame
);
464 *(AVFrame
*)data
= s
->frame
;
466 /* always report that the buffer was completely consumed */
470 static av_cold
int smc_decode_end(AVCodecContext
*avctx
)
472 SmcContext
*s
= avctx
->priv_data
;
474 if (s
->frame
.data
[0])
475 avctx
->release_buffer(avctx
, &s
->frame
);
480 AVCodec smc_decoder
= {
490 .long_name
= NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),