History

PAF demuxer and decoder

Paul B Mahol committed 12 years ago

Commit 70daeac1 parent d37c96212 files · +746 −2Browse files

Unified · Split

12 files
  1. Changelog +1 −0
  2. doc/general.texi +4 −0
  3. libavcodec/Makefile +2 −0
  4. libavcodec/allcodecs.c +2 −0
  5. libavcodec/avcodec.h +2 −0
  6. libavcodec/codec_desc.c +14 −0
  7. libavcodec/paf.c +450 −0
  8. libavcodec/version.h +1 −1
  9. libavformat/Makefile +1 −0
  10. libavformat/allformats.c +1 −0
  11. libavformat/paf.c +267 −0
  12. libavformat/version.h +1 −1
Changelog+1−0
@@ -7,6 +7,7 @@ version <next>: · 6 unmodified lines
7- replaygain data export7- replaygain data export
8- Alias PIX image encoder and decoder8- Alias PIX image encoder and decoder
9- BRender PIX image decoder9- BRender PIX image decoder
10- Amazing Studio PAF playback support
10 11
11 12
12version 10:13version 10:
doc/general.texi+4−0
@@ -179,6 +179,8 @@ library: · 178 unmodified lines
179@item American Laser Games MM @tab @tab X179@item American Laser Games MM @tab @tab X
180 @tab Multimedia format used in games like Mad Dog McCree.180 @tab Multimedia format used in games like Mad Dog McCree.
181@item 3GPP AMR @tab X @tab X181@item 3GPP AMR @tab X @tab X
182@item Amazing Studio Packed Animation File @tab @tab X
183 @tab Multimedia format used in game Heart Of Darkness.
182@item Apple HTTP Live Streaming @tab @tab X184@item Apple HTTP Live Streaming @tab @tab X
183@item ASF @tab X @tab X185@item ASF @tab X @tab X
184@item AVI @tab X @tab X186@item AVI @tab X @tab X
@@ -490,6 +492,7 @@ following image formats are supported: · 305 unmodified lines
490@item 8SVX fibonacci @tab @tab X492@item 8SVX fibonacci @tab @tab X
491@item A64 multicolor @tab X @tab493@item A64 multicolor @tab X @tab
492 @tab Creates video suitable to be played on a commodore 64 (multicolor mode).494 @tab Creates video suitable to be played on a commodore 64 (multicolor mode).
495@item Amazing Studio PAF Video @tab @tab X
493@item American Laser Games MM @tab @tab X496@item American Laser Games MM @tab @tab X
494 @tab Used in games like Mad Dog McCree.497 @tab Used in games like Mad Dog McCree.
495@item AMV Video @tab @tab X498@item AMV Video @tab @tab X
@@ -778,6 +781,7 @@ following image formats are supported: · 282 unmodified lines
778 @tab encoding supported through external library libopencore-amrnb781 @tab encoding supported through external library libopencore-amrnb
779@item AMR-WB @tab E @tab X782@item AMR-WB @tab E @tab X
780 @tab encoding supported through external library libvo-amrwbenc783 @tab encoding supported through external library libvo-amrwbenc
784@item Amazing Studio PAF Audio @tab @tab X
781@item Apple lossless audio @tab X @tab X785@item Apple lossless audio @tab X @tab X
782 @tab QuickTime fourcc 'alac'786 @tab QuickTime fourcc 'alac'
783@item ATRAC1 @tab @tab X787@item ATRAC1 @tab @tab X
libavcodec/Makefile+2−0
@@ -275,6 +275,8 @@ OBJS-$(CONFIG_MXPEG_DECODER) += mxpegdec.o · 274 unmodified lines
275OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o275OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o
276OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o276OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o
277OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o277OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o
278OBJS-$(CONFIG_PAF_VIDEO_DECODER) += paf.o
279OBJS-$(CONFIG_PAF_AUDIO_DECODER) += paf.o
278OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o280OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o
279OBJS-$(CONFIG_PAM_ENCODER) += pamenc.o281OBJS-$(CONFIG_PAM_ENCODER) += pamenc.o
280OBJS-$(CONFIG_PBM_DECODER) += pnmdec.o pnm.o282OBJS-$(CONFIG_PBM_DECODER) += pnmdec.o pnm.o
libavcodec/allcodecs.c+2−0
@@ -200,6 +200,7 @@ void avcodec_register_all(void) · 199 unmodified lines
200 REGISTER_DECODER(MTS2, mts2);200 REGISTER_DECODER(MTS2, mts2);
201 REGISTER_DECODER(MXPEG, mxpeg);201 REGISTER_DECODER(MXPEG, mxpeg);
202 REGISTER_DECODER(NUV, nuv);202 REGISTER_DECODER(NUV, nuv);
203 REGISTER_DECODER(PAF_VIDEO, paf_video);
203 REGISTER_ENCDEC (PAM, pam);204 REGISTER_ENCDEC (PAM, pam);
204 REGISTER_ENCDEC (PBM, pbm);205 REGISTER_ENCDEC (PBM, pbm);
205 REGISTER_ENCDEC (PCX, pcx);206 REGISTER_ENCDEC (PCX, pcx);
@@ -321,6 +322,7 @@ void avcodec_register_all(void) · 115 unmodified lines
321 REGISTER_DECODER(MPC7, mpc7);322 REGISTER_DECODER(MPC7, mpc7);
322 REGISTER_DECODER(MPC8, mpc8);323 REGISTER_DECODER(MPC8, mpc8);
323 REGISTER_ENCDEC (NELLYMOSER, nellymoser);324 REGISTER_ENCDEC (NELLYMOSER, nellymoser);
325 REGISTER_DECODER(PAF_AUDIO, paf_audio);
324 REGISTER_DECODER(QCELP, qcelp);326 REGISTER_DECODER(QCELP, qcelp);
325 REGISTER_DECODER(QDM2, qdm2);327 REGISTER_DECODER(QDM2, qdm2);
326 REGISTER_ENCDEC (RA_144, ra_144);328 REGISTER_ENCDEC (RA_144, ra_144);
libavcodec/avcodec.h+2−0
@@ -286,6 +286,7 @@ enum AVCodecID { · 285 unmodified lines
286 AV_CODEC_ID_FIC,286 AV_CODEC_ID_FIC,
287 AV_CODEC_ID_ALIAS_PIX,287 AV_CODEC_ID_ALIAS_PIX,
288 AV_CODEC_ID_BRENDER_PIX,288 AV_CODEC_ID_BRENDER_PIX,
289 AV_CODEC_ID_PAF_VIDEO,
289 290
290 /* various PCM "codecs" */291 /* various PCM "codecs" */
291 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs292 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
@@ -434,6 +435,7 @@ enum AVCodecID { · 142 unmodified lines
434 AV_CODEC_ID_COMFORT_NOISE,435 AV_CODEC_ID_COMFORT_NOISE,
435 AV_CODEC_ID_TAK,436 AV_CODEC_ID_TAK,
436 AV_CODEC_ID_METASOUND,437 AV_CODEC_ID_METASOUND,
438 AV_CODEC_ID_PAF_AUDIO,
437 439
438 /* subtitle codecs */440 /* subtitle codecs */
439 AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs.441 AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs.
libavcodec/codec_desc.c+14−0
@@ -1092,6 +1092,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1091 unmodified lines
1092 .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),1092 .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
1093 .props = AV_CODEC_PROP_LOSSY,1093 .props = AV_CODEC_PROP_LOSSY,
1094 },1094 },
1095 {
1096 .id = AV_CODEC_ID_PAF_VIDEO,
1097 .type = AVMEDIA_TYPE_VIDEO,
1098 .name = "paf_video",
1099 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
1100 .props = AV_CODEC_PROP_LOSSY,
1101 },
1095 1102
1096 /* image codecs */1103 /* image codecs */
1097 {1104 {
@@ -2221,6 +2228,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1123 unmodified lines
2221 .long_name = NULL_IF_CONFIG_SMALL("Voxware MetaSound"),2228 .long_name = NULL_IF_CONFIG_SMALL("Voxware MetaSound"),
2222 .props = AV_CODEC_PROP_LOSSY,2229 .props = AV_CODEC_PROP_LOSSY,
2223 },2230 },
2231 {
2232 .id = AV_CODEC_ID_PAF_AUDIO,
2233 .type = AVMEDIA_TYPE_AUDIO,
2234 .name = "paf_audio",
2235 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
2236 .props = AV_CODEC_PROP_LOSSY,
2237 },
2224 2238
2225 /* subtitle codecs */2239 /* subtitle codecs */
2226 {2240 {
libavcodec/paf.c+450−0
@@ -0,0 +1,450 @@
1/*
2 * Packed Animation File video and audio decoder
3 * Copyright (c) 2012 Paul B Mahol
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#include "libavutil/imgutils.h"
23#include "libavutil/intreadwrite.h"
24
25#include "avcodec.h"
26#include "bytestream.h"
27#include "copy_block.h"
28#include "internal.h"
29#include "mathops.h"
30
31#define PAF_SOUND_SAMPLES 2205
32#define PAF_SOUND_FRAME_SIZE ((256 + PAF_SOUND_SAMPLES) * 2)
33
34static const uint8_t block_sequences[16][8] = {
35 { 0, 0, 0, 0, 0, 0, 0, 0 }, { 2, 0, 0, 0, 0, 0, 0, 0 },
36 { 5, 7, 0, 0, 0, 0, 0, 0 }, { 5, 0, 0, 0, 0, 0, 0, 0 },
37 { 6, 0, 0, 0, 0, 0, 0, 0 }, { 5, 7, 5, 7, 0, 0, 0, 0 },
38 { 5, 7, 5, 0, 0, 0, 0, 0 }, { 5, 7, 6, 0, 0, 0, 0, 0 },
39 { 5, 5, 0, 0, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0, 0, 0, 0 },
40 { 6, 6, 0, 0, 0, 0, 0, 0 }, { 2, 4, 0, 0, 0, 0, 0, 0 },
41 { 2, 4, 5, 7, 0, 0, 0, 0 }, { 2, 4, 5, 0, 0, 0, 0, 0 },
42 { 2, 4, 6, 0, 0, 0, 0, 0 }, { 2, 4, 5, 7, 5, 7, 0, 0 },
43};
44
45typedef struct PAFVideoDecContext {
46 AVFrame *pic;
47 GetByteContext gb;
48
49 int width;
50 int height;
51
52 int current_frame;
53 uint8_t *frame[4];
54 int frame_size;
55 int video_size;
56
57 uint8_t *opcodes;
58} PAFVideoDecContext;
59
60static av_cold int paf_video_close(AVCodecContext *avctx)
61{
62 PAFVideoDecContext *c = avctx->priv_data;
63 int i;
64
65 av_frame_free(&c->pic);
66
67 for (i = 0; i < 4; i++)
68 av_freep(&c->frame[i]);
69
70 return 0;
71}
72
73static av_cold int paf_video_init(AVCodecContext *avctx)
74{
75 PAFVideoDecContext *c = avctx->priv_data;
76 int i;
77
78 c->width = avctx->width;
79 c->height = avctx->height;
80
81 if (avctx->height & 3 || avctx->width & 3) {
82 av_log(avctx, AV_LOG_ERROR,
83 "width %d and height %d must be multiplie of 4.\n",
84 avctx->width, avctx->height);
85 return AVERROR_INVALIDDATA;
86 }
87
88 avctx->pix_fmt = AV_PIX_FMT_PAL8;
89
90 c->pic = av_frame_alloc();
91 if (!c->pic)
92 return AVERROR(ENOMEM);
93
94 c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
95 c->video_size = avctx->width * avctx->height;
96 for (i = 0; i < 4; i++) {
97 c->frame[i] = av_mallocz(c->frame_size);
98 if (!c->frame[i]) {
99 paf_video_close(avctx);
100 return AVERROR(ENOMEM);
101 }
102 }
103
104 return 0;
105}
106
107static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
108{
109 int i;
110
111 for (i = 0; i < 4; i++) {
112 bytestream2_get_buffer(&c->gb, dst, 4);
113 dst += width;
114 }
115}
116
117static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
118{
119 int i;
120
121 for (i = 0; i < 4; i++) {
122 if (mask & (1 << 7 - i))
123 dst[i] = color;
124 if (mask & (1 << 3 - i))
125 dst[width + i] = color;
126 }
127}
128
129static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
130{
131 int i;
132
133 for (i = 0; i < 4; i++) {
134 if (mask & (1 << 7 - i))
135 dst[i] = src[i];
136 if (mask & (1 << 3 - i))
137 dst[width + i] = src[width + i];
138 }
139}
140
141static void set_src_position(PAFVideoDecContext *c,
142 const uint8_t **p,
143 const uint8_t **pend)
144{
145 int val = bytestream2_get_be16(&c->gb);
146 int page = val >> 14;
147 int x = (val & 0x7F);
148 int y = ((val >> 7) & 0x7F);
149
150 *p = c->frame[page] + x * 2 + y * 2 * c->width;
151 *pend = c->frame[page] + c->frame_size;
152}
153
154static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
155{
156 uint32_t opcode_size, offset;
157 uint8_t *dst, *dend, mask = 0, color = 0;
158 const uint8_t *src, *send, *opcodes;
159 int i, j, op = 0;
160
161 i = bytestream2_get_byte(&c->gb);
162 if (i) {
163 if (code & 0x10) {
164 int pos = bytestream2_tell(&c->gb) & 3;
165 if (pos)
166 bytestream2_skip(&c->gb, 4 - pos);
167 }
168 do {
169 int page, val, x, y;
170 val = bytestream2_get_be16(&c->gb);
171 page = val >> 14;
172 x = (val & 0x7F) * 2;
173 y = ((val >> 7) & 0x7F) * 2;
174 dst = c->frame[page] + x + y * c->width;
175 dend = c->frame[page] + c->frame_size;
176 offset = (x & 0x7F) * 2;
177 j = bytestream2_get_le16(&c->gb) + offset;
178 do {
179 offset++;
180 if (dst + 3 * c->width + 4 > dend)
181 return AVERROR_INVALIDDATA;
182 read4x4block(c, dst, c->width);
183 if ((offset & 0x3F) == 0)
184 dst += c->width * 3;
185 dst += 4;
186 } while (offset < j);
187 } while (--i);
188 }
189
190 dst = c->frame[c->current_frame];
191 dend = c->frame[c->current_frame] + c->frame_size;
192 do {
193 set_src_position(c, &src, &send);
194 if ((src + 3 * c->width + 4 > send) ||
195 (dst + 3 * c->width + 4 > dend))
196 return AVERROR_INVALIDDATA;
197 copy_block4(dst, src, c->width, c->width, 4);
198 i++;
199 if ((i & 0x3F) == 0)
200 dst += c->width * 3;
201 dst += 4;
202 } while (i < c->video_size / 16);
203
204 opcode_size = bytestream2_get_le16(&c->gb);
205 bytestream2_skip(&c->gb, 2);
206
207 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
208 return AVERROR_INVALIDDATA;
209
210 opcodes = pkt + bytestream2_tell(&c->gb);
211 bytestream2_skipu(&c->gb, opcode_size);
212
213 dst = c->frame[c->current_frame];
214
215 for (i = 0; i < c->height; i += 4, dst += c->width * 3)
216 for (j = 0; j < c->width; j += 4, dst += 4) {
217 int opcode, k = 0;
218 if (op > opcode_size)
219 return AVERROR_INVALIDDATA;
220 if (j & 4) {
221 opcode = opcodes[op] & 15;
222 op++;
223 } else {
224 opcode = opcodes[op] >> 4;
225 }
226
227 while (block_sequences[opcode][k]) {
228 offset = c->width * 2;
229 code = block_sequences[opcode][k++];
230
231 switch (code) {
232 case 2:
233 offset = 0;
234 case 3:
235 color = bytestream2_get_byte(&c->gb);
236 case 4:
237 mask = bytestream2_get_byte(&c->gb);
238 copy_color_mask(dst + offset, c->width, mask, color);
239 break;
240 case 5:
241 offset = 0;
242 case 6:
243 set_src_position(c, &src, &send);
244 case 7:
245 if (src + offset + c->width + 4 > send)
246 return AVERROR_INVALIDDATA;
247 mask = bytestream2_get_byte(&c->gb);
248 copy_src_mask(dst + offset, c->width, mask, src + offset);
249 break;
250 }
251 }
252 }
253
254 return 0;
255}
256
257static int paf_video_decode(AVCodecContext *avctx, void *data,
258 int *got_frame, AVPacket *pkt)
259{
260 PAFVideoDecContext *c = avctx->priv_data;
261 uint8_t code, *dst, *end;
262 int i, frame, ret;
263
264 if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
265 return ret;
266
267 bytestream2_init(&c->gb, pkt->data, pkt->size);
268
269 code = bytestream2_get_byte(&c->gb);
270 if (code & 0x20) { // frame is keyframe
271 for (i = 0; i < 4; i++)
272 memset(c->frame[i], 0, c->frame_size);
273
274 memset(c->pic->data[1], 0, AVPALETTE_SIZE);
275 c->current_frame = 0;
276 c->pic->key_frame = 1;
277 c->pic->pict_type = AV_PICTURE_TYPE_I;
278 } else {
279 c->pic->key_frame = 0;
280 c->pic->pict_type = AV_PICTURE_TYPE_P;
281 }
282
283 if (code & 0x40) { // palette update
284 uint32_t *out = (uint32_t *)c->pic->data[1];
285 int index, count;
286
287 index = bytestream2_get_byte(&c->gb);
288 count = bytestream2_get_byte(&c->gb) + 1;
289
290 if (index + count > 256)
291 return AVERROR_INVALIDDATA;
292 if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
293 return AVERROR_INVALIDDATA;
294
295 out += index;
296 for (i = 0; i < count; i++) {
297 unsigned r, g, b;
298
299 r = bytestream2_get_byteu(&c->gb);
300 r = r << 2 | r >> 4;
301 g = bytestream2_get_byteu(&c->gb);
302 g = g << 2 | g >> 4;
303 b = bytestream2_get_byteu(&c->gb);
304 b = b << 2 | b >> 4;
305 *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
306 }
307 c->pic->palette_has_changed = 1;
308 }
309
310 switch (code & 0x0F) {
311 case 0:
312 /* Block-based motion compensation using 4x4 blocks with either
313 * horizontal or vertical vectors; might incorporate VQ as well. */
314 if ((ret = decode_0(c, pkt->data, code)) < 0)
315 return ret;
316 break;
317 case 1:
318 /* Uncompressed data. This mode specifies that (width * height) bytes
319 * should be copied directly from the encoded buffer into the output. */
320 dst = c->frame[c->current_frame];
321 // possibly chunk length data
322 bytestream2_skip(&c->gb, 2);
323 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
324 return AVERROR_INVALIDDATA;
325 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
326 break;
327 case 2:
328 /* Copy reference frame: Consume the next byte in the stream as the
329 * reference frame (which should be 0, 1, 2, or 3, and should not be
330 * the same as the current frame number). */
331 frame = bytestream2_get_byte(&c->gb);
332 if (frame > 3)
333 return AVERROR_INVALIDDATA;
334 if (frame != c->current_frame)
335 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
336 break;
337 case 4:
338 /* Run length encoding.*/
339 dst = c->frame[c->current_frame];
340 end = dst + c->video_size;
341
342 bytestream2_skip(&c->gb, 2);
343
344 while (dst < end) {
345 int8_t code;
346 int count;
347
348 if (bytestream2_get_bytes_left(&c->gb) < 2)
349 return AVERROR_INVALIDDATA;
350
351 code = bytestream2_get_byteu(&c->gb);
352 count = FFABS(code) + 1;
353
354 if (dst + count > end)
355 return AVERROR_INVALIDDATA;
356 if (code < 0)
357 memset(dst, bytestream2_get_byteu(&c->gb), count);
358 else
359 bytestream2_get_buffer(&c->gb, dst, count);
360 dst += count;
361 }
362 break;
363 default:
364 avpriv_request_sample(avctx, "unknown/invalid code");
365 return AVERROR_INVALIDDATA;
366 }
367
368 av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
369 c->frame[c->current_frame], c->width,
370 c->width, c->height);
371
372 c->current_frame = (c->current_frame + 1) & 3;
373 if ((ret = av_frame_ref(data, c->pic)) < 0)
374 return ret;
375
376 *got_frame = 1;
377
378 return pkt->size;
379}
380
381static av_cold int paf_audio_init(AVCodecContext *avctx)
382{
383 if (avctx->channels != 2) {
384 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
385 return AVERROR_INVALIDDATA;
386 }
387
388 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
389 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
390
391 return 0;
392}
393
394static int paf_audio_decode(AVCodecContext *avctx, void *data,
395 int *got_frame, AVPacket *pkt)
396{
397 AVFrame *frame = data;
398 int16_t *output_samples;
399 const uint8_t *src = pkt->data;
400 int frames, ret, i, j;
401 int16_t cb[256];
402
403 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
404 if (frames < 1)
405 return AVERROR_INVALIDDATA;
406
407 frame->nb_samples = PAF_SOUND_SAMPLES * frames;
408 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
409 return ret;
410
411 output_samples = (int16_t *)frame->data[0];
412 // codebook of 256 16-bit samples and 8-bit indices to it
413 for (j = 0; j < frames; j++) {
414 for (i = 0; i < 256; i++)
415 cb[i] = sign_extend(AV_RL16(src + i * 2), 16);
416 src += 256 * 2;
417 // always 2 channels
418 for (i = 0; i < PAF_SOUND_SAMPLES * 2; i++)
419 *output_samples++ = cb[*src++];
420 }
421 *got_frame = 1;
422
423 return pkt->size;
424}
425
426#if CONFIG_PAF_VIDEO_DECODER
427AVCodec ff_paf_video_decoder = {
428 .name = "paf_video",
429 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
430 .type = AVMEDIA_TYPE_VIDEO,
431 .id = AV_CODEC_ID_PAF_VIDEO,
432 .priv_data_size = sizeof(PAFVideoDecContext),
433 .init = paf_video_init,
434 .close = paf_video_close,
435 .decode = paf_video_decode,
436 .capabilities = CODEC_CAP_DR1,
437};
438#endif
439
440#if CONFIG_PAF_AUDIO_DECODER
441AVCodec ff_paf_audio_decoder = {
442 .name = "paf_audio",
443 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
444 .type = AVMEDIA_TYPE_AUDIO,
445 .id = AV_CODEC_ID_PAF_AUDIO,
446 .init = paf_audio_init,
447 .decode = paf_audio_decode,
448 .capabilities = CODEC_CAP_DR1,
449};
450#endif
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
29#include "libavutil/version.h"29#include "libavutil/version.h"
30 30
31#define LIBAVCODEC_VERSION_MAJOR 5531#define LIBAVCODEC_VERSION_MAJOR 55
32#define LIBAVCODEC_VERSION_MINOR 3832#define LIBAVCODEC_VERSION_MINOR 39
33#define LIBAVCODEC_VERSION_MICRO 033#define LIBAVCODEC_VERSION_MICRO 0
34 34
35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
libavformat/Makefile+1−0
@@ -226,6 +226,7 @@ OBJS-$(CONFIG_OGG_MUXER) += oggenc.o \ · 225 unmodified lines
226 vorbiscomment.o226 vorbiscomment.o
227OBJS-$(CONFIG_OMA_DEMUXER) += omadec.o pcm.o oma.o227OBJS-$(CONFIG_OMA_DEMUXER) += omadec.o pcm.o oma.o
228OBJS-$(CONFIG_OMA_MUXER) += omaenc.o rawenc.o oma.o id3v2enc.o228OBJS-$(CONFIG_OMA_MUXER) += omaenc.o rawenc.o oma.o id3v2enc.o
229OBJS-$(CONFIG_PAF_DEMUXER) += paf.o
229OBJS-$(CONFIG_PCM_ALAW_DEMUXER) += pcmdec.o pcm.o230OBJS-$(CONFIG_PCM_ALAW_DEMUXER) += pcmdec.o pcm.o
230OBJS-$(CONFIG_PCM_ALAW_MUXER) += pcmenc.o rawenc.o231OBJS-$(CONFIG_PCM_ALAW_MUXER) += pcmenc.o rawenc.o
231OBJS-$(CONFIG_PCM_F32BE_DEMUXER) += pcmdec.o pcm.o232OBJS-$(CONFIG_PCM_F32BE_DEMUXER) += pcmdec.o pcm.o
libavformat/allformats.c+1−0
@@ -176,6 +176,7 @@ void av_register_all(void) · 175 unmodified lines
176 REGISTER_DEMUXER (NUV, nuv);176 REGISTER_DEMUXER (NUV, nuv);
177 REGISTER_MUXDEMUX(OGG, ogg);177 REGISTER_MUXDEMUX(OGG, ogg);
178 REGISTER_MUXDEMUX(OMA, oma);178 REGISTER_MUXDEMUX(OMA, oma);
179 REGISTER_DEMUXER (PAF, paf);
179 REGISTER_MUXDEMUX(PCM_ALAW, pcm_alaw);180 REGISTER_MUXDEMUX(PCM_ALAW, pcm_alaw);
180 REGISTER_MUXDEMUX(PCM_MULAW, pcm_mulaw);181 REGISTER_MUXDEMUX(PCM_MULAW, pcm_mulaw);
181 REGISTER_MUXDEMUX(PCM_F64BE, pcm_f64be);182 REGISTER_MUXDEMUX(PCM_F64BE, pcm_f64be);
libavformat/paf.c+267−0
@@ -0,0 +1,267 @@
1/*
2 * Packed Animation File demuxer
3 * Copyright (c) 2012 Paul B Mahol
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#include "libavutil/channel_layout.h"
23
24#include "avformat.h"
25#include "internal.h"
26
27#define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
28#define PAF_SOUND_SAMPLES 2205
29#define PAF_SOUND_FRAME_SIZE ((256 + PAF_SOUND_SAMPLES) * 2)
30
31typedef struct {
32 uint32_t buffer_size;
33 uint32_t frame_blks;
34 uint32_t nb_frames;
35 uint32_t start_offset;
36 uint32_t preload_count;
37 uint32_t max_video_blks;
38 uint32_t max_audio_blks;
39
40 uint32_t current_frame;
41 uint32_t current_frame_count;
42 uint32_t current_frame_block;
43
44 uint32_t *blocks_count_table;
45 uint32_t *frames_offset_table;
46 uint32_t *blocks_offset_table;
47
48 uint8_t *video_frame;
49 int video_size;
50
51 uint8_t *audio_frame;
52 uint8_t *temp_audio_frame;
53 int audio_size;
54
55 int got_audio;
56} PAFDemuxContext;
57
58static int read_probe(AVProbeData *p)
59{
60 if ((p->buf_size >= strlen(MAGIC)) &&
61 !memcmp(p->buf, MAGIC, strlen(MAGIC)))
62 return AVPROBE_SCORE_MAX;
63 return 0;
64}
65
66static int read_close(AVFormatContext *s)
67{
68 PAFDemuxContext *p = s->priv_data;
69
70 av_freep(&p->blocks_count_table);
71 av_freep(&p->frames_offset_table);
72 av_freep(&p->blocks_offset_table);
73 av_freep(&p->video_frame);
74 av_freep(&p->audio_frame);
75 av_freep(&p->temp_audio_frame);
76
77 return 0;
78}
79
80static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
81{
82 int i;
83
84 for (i = 0; i < count; i++)
85 table[i] = avio_rl32(s->pb);
86
87 avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
88}
89
90static int read_header(AVFormatContext *s)
91{
92 PAFDemuxContext *p = s->priv_data;
93 AVIOContext *pb = s->pb;
94 AVStream *ast, *vst;
95 int ret = 0;
96
97 avio_skip(pb, 132);
98
99 vst = avformat_new_stream(s, 0);
100 if (!vst)
101 return AVERROR(ENOMEM);
102
103 vst->start_time = 0;
104 vst->nb_frames =
105 vst->duration =
106 p->nb_frames = avio_rl32(pb);
107 avio_skip(pb, 4);
108
109 vst->codec->width = avio_rl32(pb);
110 vst->codec->height = avio_rl32(pb);
111 avio_skip(pb, 4);
112
113 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
114 vst->codec->codec_tag = 0;
115 vst->codec->codec_id = AV_CODEC_ID_PAF_VIDEO;
116 avpriv_set_pts_info(vst, 64, 1, 10);
117
118 ast = avformat_new_stream(s, 0);
119 if (!ast)
120 return AVERROR(ENOMEM);
121
122 ast->start_time = 0;
123 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
124 ast->codec->codec_tag = 0;
125 ast->codec->codec_id = AV_CODEC_ID_PAF_AUDIO;
126 ast->codec->channels = 2;
127 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
128 ast->codec->sample_rate = 22050;
129 avpriv_set_pts_info(ast, 64, 1, 22050);
130
131 p->buffer_size = avio_rl32(pb);
132 p->preload_count = avio_rl32(pb);
133 p->frame_blks = avio_rl32(pb);
134 p->start_offset = avio_rl32(pb);
135 p->max_video_blks = avio_rl32(pb);
136 p->max_audio_blks = avio_rl32(pb);
137 if (p->buffer_size < 175 ||
138 p->max_audio_blks < 2 ||
139 p->max_video_blks < 1 ||
140 p->frame_blks < 1 ||
141 p->nb_frames < 1 ||
142 p->preload_count < 1 ||
143 p->buffer_size > 2048 ||
144 p->max_video_blks > 2048 ||
145 p->max_audio_blks > 2048 ||
146 p->nb_frames > INT_MAX / sizeof(uint32_t) ||
147 p->frame_blks > INT_MAX / sizeof(uint32_t))
148 return AVERROR_INVALIDDATA;
149
150 p->blocks_count_table = av_mallocz(p->nb_frames *
151 sizeof(*p->blocks_count_table));
152 p->frames_offset_table = av_mallocz(p->nb_frames *
153 sizeof(*p->frames_offset_table));
154 p->blocks_offset_table = av_mallocz(p->frame_blks *
155 sizeof(*p->blocks_offset_table));
156
157 p->video_size = p->max_video_blks * p->buffer_size;
158 p->video_frame = av_mallocz(p->video_size);
159
160 p->audio_size = p->max_audio_blks * p->buffer_size;
161 p->audio_frame = av_mallocz(p->audio_size);
162 p->temp_audio_frame = av_mallocz(p->audio_size);
163
164 if (!p->blocks_count_table ||
165 !p->frames_offset_table ||
166 !p->blocks_offset_table ||
167 !p->video_frame ||
168 !p->audio_frame ||
169 !p->temp_audio_frame) {
170 ret = AVERROR(ENOMEM);
171 goto fail;
172 }
173
174 avio_seek(pb, p->buffer_size, SEEK_SET);
175
176 read_table(s, p->blocks_count_table, p->nb_frames);
177 read_table(s, p->frames_offset_table, p->nb_frames);
178 read_table(s, p->blocks_offset_table, p->frame_blks);
179
180 p->got_audio = 0;
181 p->current_frame = 0;
182 p->current_frame_block = 0;
183
184 avio_seek(pb, p->start_offset, SEEK_SET);
185
186 return 0;
187
188fail:
189 read_close(s);
190
191 return ret;
192}
193
194static int read_packet(AVFormatContext *s, AVPacket *pkt)
195{
196 PAFDemuxContext *p = s->priv_data;
197 AVIOContext *pb = s->pb;
198 uint32_t count, offset;
199 int size, i;
200
201 if (p->current_frame >= p->nb_frames || pb->eof_reached)
202 return AVERROR_EOF;
203
204 if (p->got_audio) {
205 if (av_new_packet(pkt, p->audio_size) < 0)
206 return AVERROR(ENOMEM);
207
208 memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
209 pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
210 pkt->flags |= AV_PKT_FLAG_KEY;
211 pkt->stream_index = 1;
212 p->got_audio = 0;
213 return pkt->size;
214 }
215
216 count = (p->current_frame == 0) ? p->preload_count
217 : p->blocks_count_table[p->current_frame - 1];
218 for (i = 0; i < count; i++) {
219 if (p->current_frame_block >= p->frame_blks)
220 return AVERROR_INVALIDDATA;
221
222 offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
223 if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
224 if (offset > p->audio_size - p->buffer_size)
225 return AVERROR_INVALIDDATA;
226
227 avio_read(pb, p->audio_frame + offset, p->buffer_size);
228 if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
229 memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
230 p->got_audio = 1;
231 }
232 } else {
233 if (offset > p->video_size - p->buffer_size)
234 return AVERROR_INVALIDDATA;
235
236 avio_read(pb, p->video_frame + offset, p->buffer_size);
237 }
238 p->current_frame_block++;
239 }
240
241 if (p->frames_offset_table[p->current_frame] >= p->video_size)
242 return AVERROR_INVALIDDATA;
243
244 size = p->video_size - p->frames_offset_table[p->current_frame];
245
246 if (av_new_packet(pkt, size) < 0)
247 return AVERROR(ENOMEM);
248
249 pkt->stream_index = 0;
250 pkt->duration = 1;
251 memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
252 if (pkt->data[0] & 0x20)
253 pkt->flags |= AV_PKT_FLAG_KEY;
254 p->current_frame++;
255
256 return pkt->size;
257}
258
259AVInputFormat ff_paf_demuxer = {
260 .name = "paf",
261 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
262 .priv_data_size = sizeof(PAFDemuxContext),
263 .read_probe = read_probe,
264 .read_header = read_header,
265 .read_packet = read_packet,
266 .read_close = read_close,
267};
libavformat/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
30#include "libavutil/version.h"30#include "libavutil/version.h"
31 31
32#define LIBAVFORMAT_VERSION_MAJOR 5532#define LIBAVFORMAT_VERSION_MAJOR 55
33#define LIBAVFORMAT_VERSION_MINOR 1333#define LIBAVFORMAT_VERSION_MINOR 14
34#define LIBAVFORMAT_VERSION_MICRO 034#define LIBAVFORMAT_VERSION_MICRO 0
35 35
36#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \36#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t