FFmpeg  4.4.9
ipmovie.c
Go to the documentation of this file.
1 /*
2  * Interplay MVE File Demuxer
3  * Copyright (c) 2003 The FFmpeg project
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * Interplay MVE file demuxer
25  * by Mike Melanson (melanson@pcisys.net)
26  * For more information regarding the Interplay MVE file format, visit:
27  * http://www.pcisys.net/~melanson/codecs/
28  * The aforementioned site also contains a command line utility for parsing
29  * IP MVE files so that you can get a good idea of the typical structure of
30  * such files. This demuxer is not the best example to use if you are trying
31  * to write your own as it uses a rather roundabout approach for splitting
32  * up and sending out the chunks.
33  */
34 
36 #include "libavutil/intreadwrite.h"
37 #include "avformat.h"
38 #include "internal.h"
39 
40 #define CHUNK_PREAMBLE_SIZE 4
41 #define OPCODE_PREAMBLE_SIZE 4
42 
43 #define CHUNK_INIT_AUDIO 0x0000
44 #define CHUNK_AUDIO_ONLY 0x0001
45 #define CHUNK_INIT_VIDEO 0x0002
46 #define CHUNK_VIDEO 0x0003
47 #define CHUNK_SHUTDOWN 0x0004
48 #define CHUNK_END 0x0005
49 /* these last types are used internally */
50 #define CHUNK_HAVE_PACKET 0xFFFB
51 #define CHUNK_DONE 0xFFFC
52 #define CHUNK_NOMEM 0xFFFD
53 #define CHUNK_EOF 0xFFFE
54 #define CHUNK_BAD 0xFFFF
55 
56 #define OPCODE_END_OF_STREAM 0x00
57 #define OPCODE_END_OF_CHUNK 0x01
58 #define OPCODE_CREATE_TIMER 0x02
59 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
60 #define OPCODE_START_STOP_AUDIO 0x04
61 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
62 #define OPCODE_VIDEO_DATA_06 0x06
63 #define OPCODE_SEND_BUFFER 0x07
64 #define OPCODE_AUDIO_FRAME 0x08
65 #define OPCODE_SILENCE_FRAME 0x09
66 #define OPCODE_INIT_VIDEO_MODE 0x0A
67 #define OPCODE_CREATE_GRADIENT 0x0B
68 #define OPCODE_SET_PALETTE 0x0C
69 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
70 #define OPCODE_SET_SKIP_MAP 0x0E
71 #define OPCODE_SET_DECODING_MAP 0x0F
72 #define OPCODE_VIDEO_DATA_10 0x10
73 #define OPCODE_VIDEO_DATA_11 0x11
74 #define OPCODE_UNKNOWN_12 0x12
75 #define OPCODE_UNKNOWN_13 0x13
76 #define OPCODE_UNKNOWN_14 0x14
77 #define OPCODE_UNKNOWN_15 0x15
78 
79 #define PALETTE_COUNT 256
80 
81 typedef struct IPMVEContext {
83  unsigned char *buf;
84  int buf_size;
85 
86  uint64_t frame_pts_inc;
87 
88  unsigned int video_bpp;
89  unsigned int video_width;
90  unsigned int video_height;
92  uint32_t palette[256];
94  int changed;
97 
98  unsigned int audio_bits;
99  unsigned int audio_channels;
100  unsigned int audio_sample_rate;
101  enum AVCodecID audio_type;
102  unsigned int audio_frame_count;
103 
106 
115 
117 
118 } IPMVEContext;
119 
121  AVPacket *pkt) {
122 
123  int chunk_type;
124 
125  if (s->audio_chunk_offset && s->audio_channels && s->audio_bits) {
126  if (s->audio_type == AV_CODEC_ID_NONE) {
127  av_log(s->avf, AV_LOG_ERROR, "Can not read audio packet before"
128  "audio codec is known\n");
129  return CHUNK_BAD;
130  }
131 
132  /* adjust for PCM audio by skipping chunk header */
133  if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
134  s->audio_chunk_offset += 6;
135  s->audio_chunk_size -= 6;
136  }
137 
138  avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
139  s->audio_chunk_offset = 0;
140 
141  if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
142  return CHUNK_EOF;
143 
144  pkt->stream_index = s->audio_stream_index;
145  pkt->pts = s->audio_frame_count;
146 
147  /* audio frame maintenance */
148  if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
149  s->audio_frame_count +=
150  (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
151  else
152  s->audio_frame_count +=
153  (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
154 
155  av_log(s->avf, AV_LOG_TRACE, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
156  pkt->pts, s->audio_frame_count);
157 
158  chunk_type = CHUNK_HAVE_PACKET;
159 
160  } else if (s->frame_format) {
161 
162  /* send the frame format, decode map, the video data, skip map, and the send_buffer flag together */
163 
164  if (av_new_packet(pkt, 8 + s->decode_map_chunk_size + s->video_chunk_size + s->skip_map_chunk_size))
165  return CHUNK_NOMEM;
166 
167  if (s->has_palette) {
168  uint8_t *pal;
169 
172  if (pal) {
173  memcpy(pal, s->palette, AVPALETTE_SIZE);
174  s->has_palette = 0;
175  }
176  }
177 
178  if (s->changed) {
179  ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
180  s->changed = 0;
181  }
182 
183  AV_WL8(pkt->data, s->frame_format);
184  AV_WL8(pkt->data + 1, s->send_buffer);
185  AV_WL16(pkt->data + 2, s->video_chunk_size);
186  AV_WL16(pkt->data + 4, s->decode_map_chunk_size);
187  AV_WL16(pkt->data + 6, s->skip_map_chunk_size);
188 
189  s->frame_format = 0;
190  s->send_buffer = 0;
191 
192  pkt->pos = s->video_chunk_offset;
193  avio_seek(pb, s->video_chunk_offset, SEEK_SET);
194  s->video_chunk_offset = 0;
195 
196  if (avio_read(pb, pkt->data + 8, s->video_chunk_size) !=
197  s->video_chunk_size) {
199  return CHUNK_EOF;
200  }
201 
202  if (s->decode_map_chunk_size) {
203  pkt->pos = s->decode_map_chunk_offset;
204  avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
205  s->decode_map_chunk_offset = 0;
206 
207  if (avio_read(pb, pkt->data + 8 + s->video_chunk_size,
208  s->decode_map_chunk_size) != s->decode_map_chunk_size) {
210  return CHUNK_EOF;
211  }
212  }
213 
214  if (s->skip_map_chunk_size) {
215  pkt->pos = s->skip_map_chunk_offset;
216  avio_seek(pb, s->skip_map_chunk_offset, SEEK_SET);
217  s->skip_map_chunk_offset = 0;
218 
219  if (avio_read(pb, pkt->data + 8 + s->video_chunk_size + s->decode_map_chunk_size,
220  s->skip_map_chunk_size) != s->skip_map_chunk_size) {
222  return CHUNK_EOF;
223  }
224  }
225 
226  s->video_chunk_size = 0;
227  s->decode_map_chunk_size = 0;
228  s->skip_map_chunk_size = 0;
229 
230  pkt->stream_index = s->video_stream_index;
231  pkt->pts = s->video_pts;
232 
233  av_log(s->avf, AV_LOG_TRACE, "sending video frame with pts %"PRId64"\n", pkt->pts);
234 
235  s->video_pts += s->frame_pts_inc;
236 
237  chunk_type = CHUNK_HAVE_PACKET;
238 
239  } else {
240 
241  avio_seek(pb, s->next_chunk_offset, SEEK_SET);
242  chunk_type = CHUNK_DONE;
243 
244  }
245 
246  return chunk_type;
247 }
248 
250 {
251  IPMVEContext *ipmovie = s->priv_data;
253  if (!st)
254  return AVERROR(ENOMEM);
255  avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
256  ipmovie->audio_stream_index = st->index;
258  st->codecpar->codec_id = ipmovie->audio_type;
259  st->codecpar->codec_tag = 0; /* no tag */
260  st->codecpar->channels = ipmovie->audio_channels;
263  st->codecpar->sample_rate = ipmovie->audio_sample_rate;
264  st->codecpar->bits_per_coded_sample = ipmovie->audio_bits;
268  st->codecpar->bit_rate /= 2;
270 
271  return 0;
272 }
273 
274 /* This function loads and processes a single chunk in an IP movie file.
275  * It returns the type of chunk that was processed. */
277  AVPacket *pkt)
278 {
279  unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
280  int chunk_type;
281  int chunk_size;
282  unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
283  unsigned char opcode_type;
284  unsigned char opcode_version;
285  int opcode_size;
286  unsigned char scratch[1024];
287  int i, j;
288  int first_color, last_color;
289  int audio_flags;
290  unsigned char r, g, b;
291  unsigned int width, height;
292 
293  /* see if there are any pending packets */
294  chunk_type = load_ipmovie_packet(s, pb, pkt);
295  if (chunk_type != CHUNK_DONE)
296  return chunk_type;
297 
298  /* read the next chunk, wherever the file happens to be pointing */
299  if (avio_feof(pb))
300  return CHUNK_EOF;
301  if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
303  return CHUNK_BAD;
304  chunk_size = AV_RL16(&chunk_preamble[0]);
305  chunk_type = AV_RL16(&chunk_preamble[2]);
306 
307  av_log(s->avf, AV_LOG_TRACE, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
308 
309  switch (chunk_type) {
310 
311  case CHUNK_INIT_AUDIO:
312  av_log(s->avf, AV_LOG_TRACE, "initialize audio\n");
313  break;
314 
315  case CHUNK_AUDIO_ONLY:
316  av_log(s->avf, AV_LOG_TRACE, "audio only\n");
317  break;
318 
319  case CHUNK_INIT_VIDEO:
320  av_log(s->avf, AV_LOG_TRACE, "initialize video\n");
321  break;
322 
323  case CHUNK_VIDEO:
324  av_log(s->avf, AV_LOG_TRACE, "video (and audio)\n");
325  break;
326 
327  case CHUNK_SHUTDOWN:
328  av_log(s->avf, AV_LOG_TRACE, "shutdown\n");
329  break;
330 
331  case CHUNK_END:
332  av_log(s->avf, AV_LOG_TRACE, "end\n");
333  break;
334 
335  default:
336  av_log(s->avf, AV_LOG_TRACE, "invalid chunk\n");
337  chunk_type = CHUNK_BAD;
338  break;
339 
340  }
341 
342  while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
343 
344  /* read the next chunk, wherever the file happens to be pointing */
345  if (avio_feof(pb)) {
346  chunk_type = CHUNK_EOF;
347  break;
348  }
349  if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
351  chunk_type = CHUNK_BAD;
352  break;
353  }
354 
355  opcode_size = AV_RL16(&opcode_preamble[0]);
356  opcode_type = opcode_preamble[2];
357  opcode_version = opcode_preamble[3];
358 
359  chunk_size -= OPCODE_PREAMBLE_SIZE;
360  chunk_size -= opcode_size;
361  if (chunk_size < 0) {
362  av_log(s->avf, AV_LOG_TRACE, "chunk_size countdown just went negative\n");
363  chunk_type = CHUNK_BAD;
364  break;
365  }
366 
367  av_log(s->avf, AV_LOG_TRACE, " opcode type %02X, version %d, 0x%04X bytes: ",
368  opcode_type, opcode_version, opcode_size);
369  switch (opcode_type) {
370 
372  av_log(s->avf, AV_LOG_TRACE, "end of stream\n");
373  avio_skip(pb, opcode_size);
374  break;
375 
376  case OPCODE_END_OF_CHUNK:
377  av_log(s->avf, AV_LOG_TRACE, "end of chunk\n");
378  avio_skip(pb, opcode_size);
379  break;
380 
381  case OPCODE_CREATE_TIMER:
382  av_log(s->avf, AV_LOG_TRACE, "create timer\n");
383  if ((opcode_version > 0) || (opcode_size != 6)) {
384  av_log(s->avf, AV_LOG_TRACE, "bad create_timer opcode\n");
385  chunk_type = CHUNK_BAD;
386  break;
387  }
388  if (avio_read(pb, scratch, opcode_size) !=
389  opcode_size) {
390  chunk_type = CHUNK_BAD;
391  break;
392  }
393  s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
394  break;
395 
397  av_log(s->avf, AV_LOG_TRACE, "initialize audio buffers\n");
398  if (opcode_version > 1 || opcode_size > 10 || opcode_size < 6) {
399  av_log(s->avf, AV_LOG_TRACE, "bad init_audio_buffers opcode\n");
400  chunk_type = CHUNK_BAD;
401  break;
402  }
403  if (avio_read(pb, scratch, opcode_size) !=
404  opcode_size) {
405  chunk_type = CHUNK_BAD;
406  break;
407  }
408  s->audio_sample_rate = AV_RL16(&scratch[4]);
409  audio_flags = AV_RL16(&scratch[2]);
410  /* bit 0 of the flags: 0 = mono, 1 = stereo */
411  s->audio_channels = (audio_flags & 1) + 1;
412  /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
413  s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
414  /* bit 2 indicates compressed audio in version 1 opcode */
415  if ((opcode_version == 1) && (audio_flags & 0x4))
416  s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;
417  else if (s->audio_bits == 16)
418  s->audio_type = AV_CODEC_ID_PCM_S16LE;
419  else
420  s->audio_type = AV_CODEC_ID_PCM_U8;
421  av_log(s->avf, AV_LOG_TRACE, "audio: %d bits, %d Hz, %s, %s format\n",
422  s->audio_bits, s->audio_sample_rate,
423  (s->audio_channels == 2) ? "stereo" : "mono",
424  (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?
425  "Interplay audio" : "PCM");
426  break;
427 
429  av_log(s->avf, AV_LOG_TRACE, "start/stop audio\n");
430  avio_skip(pb, opcode_size);
431  break;
432 
434  av_log(s->avf, AV_LOG_TRACE, "initialize video buffers\n");
435  if ((opcode_version > 2) || (opcode_size > 8) || opcode_size < 4
436  || opcode_version == 2 && opcode_size < 8
437  ) {
438  av_log(s->avf, AV_LOG_TRACE, "bad init_video_buffers opcode\n");
439  chunk_type = CHUNK_BAD;
440  break;
441  }
442  if (avio_read(pb, scratch, opcode_size) !=
443  opcode_size) {
444  chunk_type = CHUNK_BAD;
445  break;
446  }
447  width = AV_RL16(&scratch[0]) * 8;
448  height = AV_RL16(&scratch[2]) * 8;
449  if (width != s->video_width) {
450  s->video_width = width;
451  s->changed++;
452  }
453  if (height != s->video_height) {
454  s->video_height = height;
455  s->changed++;
456  }
457  if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
458  s->video_bpp = 8;
459  } else {
460  s->video_bpp = 16;
461  }
462  av_log(s->avf, AV_LOG_TRACE, "video resolution: %d x %d\n",
463  s->video_width, s->video_height);
464  break;
465 
466  case OPCODE_UNKNOWN_12:
467  case OPCODE_UNKNOWN_13:
468  case OPCODE_UNKNOWN_14:
469  case OPCODE_UNKNOWN_15:
470  av_log(s->avf, AV_LOG_TRACE, "unknown (but documented) opcode %02X\n", opcode_type);
471  avio_skip(pb, opcode_size);
472  break;
473 
474  case OPCODE_SEND_BUFFER:
475  av_log(s->avf, AV_LOG_TRACE, "send buffer\n");
476  avio_skip(pb, opcode_size);
477  s->send_buffer = 1;
478  break;
479 
480  case OPCODE_AUDIO_FRAME:
481  av_log(s->avf, AV_LOG_TRACE, "audio frame\n");
482 
483  /* log position and move on for now */
484  s->audio_chunk_offset = avio_tell(pb);
485  s->audio_chunk_size = opcode_size;
486  avio_skip(pb, opcode_size);
487  break;
488 
490  av_log(s->avf, AV_LOG_TRACE, "silence frame\n");
491  avio_skip(pb, opcode_size);
492  break;
493 
495  av_log(s->avf, AV_LOG_TRACE, "initialize video mode\n");
496  avio_skip(pb, opcode_size);
497  break;
498 
500  av_log(s->avf, AV_LOG_TRACE, "create gradient\n");
501  avio_skip(pb, opcode_size);
502  break;
503 
504  case OPCODE_SET_PALETTE:
505  av_log(s->avf, AV_LOG_TRACE, "set palette\n");
506  /* check for the logical maximum palette size
507  * (3 * 256 + 4 bytes) */
508  if (opcode_size > 0x304 || opcode_size < 4) {
509  av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette opcode with invalid size\n");
510  chunk_type = CHUNK_BAD;
511  break;
512  }
513  if (avio_read(pb, scratch, opcode_size) != opcode_size) {
514  chunk_type = CHUNK_BAD;
515  break;
516  }
517 
518  /* load the palette into internal data structure */
519  first_color = AV_RL16(&scratch[0]);
520  last_color = first_color + AV_RL16(&scratch[2]) - 1;
521  /* sanity check (since they are 16 bit values) */
522  if ( (first_color > 0xFF) || (last_color > 0xFF)
523  || (last_color - first_color + 1)*3 + 4 > opcode_size) {
524  av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
525  first_color, last_color);
526  chunk_type = CHUNK_BAD;
527  break;
528  }
529  j = 4; /* offset of first palette data */
530  for (i = first_color; i <= last_color; i++) {
531  /* the palette is stored as a 6-bit VGA palette, thus each
532  * component is shifted up to a 8-bit range */
533  r = scratch[j++] * 4;
534  g = scratch[j++] * 4;
535  b = scratch[j++] * 4;
536  s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
537  s->palette[i] |= s->palette[i] >> 6 & 0x30303;
538  }
539  s->has_palette = 1;
540  break;
541 
543  av_log(s->avf, AV_LOG_TRACE, "set palette compressed\n");
544  avio_skip(pb, opcode_size);
545  break;
546 
547  case OPCODE_SET_SKIP_MAP:
548  av_log(s->avf, AV_LOG_TRACE, "set skip map\n");
549 
550  /* log position and move on for now */
551  s->skip_map_chunk_offset = avio_tell(pb);
552  s->skip_map_chunk_size = opcode_size;
553  avio_skip(pb, opcode_size);
554  break;
555 
557  av_log(s->avf, AV_LOG_TRACE, "set decoding map\n");
558 
559  /* log position and move on for now */
560  s->decode_map_chunk_offset = avio_tell(pb);
561  s->decode_map_chunk_size = opcode_size;
562  avio_skip(pb, opcode_size);
563  break;
564 
566  av_log(s->avf, AV_LOG_TRACE, "set video data format 0x06\n");
567  s->frame_format = 0x06;
568 
569  /* log position and move on for now */
570  s->video_chunk_offset = avio_tell(pb);
571  s->video_chunk_size = opcode_size;
572  avio_skip(pb, opcode_size);
573  break;
574 
576  av_log(s->avf, AV_LOG_TRACE, "set video data format 0x10\n");
577  s->frame_format = 0x10;
578 
579  /* log position and move on for now */
580  s->video_chunk_offset = avio_tell(pb);
581  s->video_chunk_size = opcode_size;
582  avio_skip(pb, opcode_size);
583  break;
584 
586  av_log(s->avf, AV_LOG_TRACE, "set video data format 0x11\n");
587  s->frame_format = 0x11;
588 
589  /* log position and move on for now */
590  s->video_chunk_offset = avio_tell(pb);
591  s->video_chunk_size = opcode_size;
592  avio_skip(pb, opcode_size);
593  break;
594 
595  default:
596  av_log(s->avf, AV_LOG_TRACE, "*** unknown opcode type\n");
597  chunk_type = CHUNK_BAD;
598  break;
599 
600  }
601  }
602 
603  if (s->avf->nb_streams == 1 && s->audio_type)
604  init_audio(s->avf);
605 
606  /* make a note of where the stream is sitting */
607  s->next_chunk_offset = avio_tell(pb);
608 
609  return chunk_type;
610 }
611 
612 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
613 
614 static int ipmovie_probe(const AVProbeData *p)
615 {
616  const uint8_t *b = p->buf;
617  const uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
618  do {
619  if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
620  return AVPROBE_SCORE_MAX;
621  b++;
622  } while (b < b_end);
623 
624  return 0;
625 }
626 
628 {
629  IPMVEContext *ipmovie = s->priv_data;
630  AVIOContext *pb = s->pb;
631  AVStream *st;
632  unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
633  int chunk_type, i;
634  uint8_t signature_buffer[sizeof(signature)];
635 
636  ipmovie->avf = s;
637 
638  if (avio_read(pb, signature_buffer, sizeof(signature_buffer)) != sizeof(signature_buffer))
639  return AVERROR_INVALIDDATA;
640  while (memcmp(signature_buffer, signature, sizeof(signature))) {
641  memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
642  signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
643  if (avio_feof(pb))
644  return AVERROR_EOF;
645  }
646  /* initialize private context members */
647  ipmovie->video_pts = ipmovie->audio_frame_count = 0;
648  ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
649  ipmovie->decode_map_chunk_offset = ipmovie->skip_map_chunk_offset = 0;
650  ipmovie->decode_map_chunk_size = ipmovie->video_chunk_size =
651  ipmovie->skip_map_chunk_size = 0;
652  ipmovie->send_buffer = ipmovie->frame_format = 0;
653 
654  /* on the first read, this will position the stream at the first chunk */
655  ipmovie->next_chunk_offset = avio_tell(pb) + 4;
656 
657  for (i = 0; i < 256; i++)
658  ipmovie->palette[i] = 0xFFU << 24;
659 
660  /* process the first chunk which should be CHUNK_INIT_VIDEO */
661  if (process_ipmovie_chunk(ipmovie, pb, NULL) != CHUNK_INIT_VIDEO) {
662  return AVERROR_INVALIDDATA;
663  }
664 
665  /* peek ahead to the next chunk-- if it is an init audio chunk, process
666  * it; if it is the first video chunk, this is a silent file */
667  if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
669  return AVERROR(EIO);
670  chunk_type = AV_RL16(&chunk_preamble[2]);
671  avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
672 
673  if (chunk_type == CHUNK_VIDEO)
674  ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
675  else if (process_ipmovie_chunk(ipmovie, pb, s->internal->parse_pkt) != CHUNK_INIT_AUDIO) {
676  return AVERROR_INVALIDDATA;
677  }
678 
679  /* initialize the stream decoders */
680  st = avformat_new_stream(s, NULL);
681  if (!st)
682  return AVERROR(ENOMEM);
683  avpriv_set_pts_info(st, 63, 1, 1000000);
684  ipmovie->video_stream_index = st->index;
687  st->codecpar->codec_tag = 0; /* no fourcc */
688  st->codecpar->width = ipmovie->video_width;
689  st->codecpar->height = ipmovie->video_height;
690  st->codecpar->bits_per_coded_sample = ipmovie->video_bpp;
691 
692  if (ipmovie->audio_type) {
693  return init_audio(s);
694  } else
695  s->ctx_flags |= AVFMTCTX_NOHEADER;
696 
697  return 0;
698 }
699 
701  AVPacket *pkt)
702 {
703  IPMVEContext *ipmovie = s->priv_data;
704  AVIOContext *pb = s->pb;
705  int ret;
706 
707  for (;;) {
708  ret = process_ipmovie_chunk(ipmovie, pb, pkt);
709  /* dispatch the first of any pending packets */
710  if ((ret == CHUNK_VIDEO) || (ret == CHUNK_AUDIO_ONLY))
711  ret = load_ipmovie_packet(ipmovie, pb, pkt);
712 
713  if (ret == CHUNK_BAD)
714  ret = AVERROR_INVALIDDATA;
715  else if (ret == CHUNK_EOF)
716  ret = AVERROR(EIO);
717  else if (ret == CHUNK_NOMEM)
718  ret = AVERROR(ENOMEM);
719  else if (ret == CHUNK_END || ret == CHUNK_SHUTDOWN)
720  ret = AVERROR_EOF;
721  else if (ret == CHUNK_HAVE_PACKET)
722  ret = 0;
723  else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
724  continue;
725  else
726  continue;
727 
728  return ret;
729  }
730 }
731 
733  .name = "ipmovie",
734  .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
735  .priv_data_size = sizeof(IPMVEContext),
739 };
uint8_t
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1177
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:310
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:371
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:343
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RL32
Definition: intreadwrite.h:146
#define s(width, name)
Definition: cbs_vp9.c:257
audio channel layout utility functions
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_INTERPLAY_DPCM
Definition: codec_id.h:415
@ AV_CODEC_ID_INTERPLAY_VIDEO
Definition: codec_id.h:88
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:46
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int i
Definition: input.c:407
#define AV_WL8(p, d)
Definition: intreadwrite.h:399
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define CHUNK_DONE
Definition: ipmovie.c:51
#define CHUNK_BAD
Definition: ipmovie.c:54
AVInputFormat ff_ipmovie_demuxer
Definition: ipmovie.c:732
#define OPCODE_INIT_VIDEO_BUFFERS
Definition: ipmovie.c:61
#define OPCODE_UNKNOWN_12
Definition: ipmovie.c:74
#define CHUNK_SHUTDOWN
Definition: ipmovie.c:47
#define OPCODE_SILENCE_FRAME
Definition: ipmovie.c:65
#define OPCODE_SET_SKIP_MAP
Definition: ipmovie.c:70
#define OPCODE_INIT_VIDEO_MODE
Definition: ipmovie.c:66
#define OPCODE_INIT_AUDIO_BUFFERS
Definition: ipmovie.c:59
#define OPCODE_VIDEO_DATA_11
Definition: ipmovie.c:73
#define OPCODE_VIDEO_DATA_06
Definition: ipmovie.c:62
#define CHUNK_INIT_AUDIO
Definition: ipmovie.c:43
#define CHUNK_INIT_VIDEO
Definition: ipmovie.c:45
#define OPCODE_SEND_BUFFER
Definition: ipmovie.c:63
#define OPCODE_SET_PALETTE
Definition: ipmovie.c:68
#define CHUNK_NOMEM
Definition: ipmovie.c:52
static int init_audio(AVFormatContext *s)
Definition: ipmovie.c:249
#define OPCODE_PREAMBLE_SIZE
Definition: ipmovie.c:41
static const char signature[]
Definition: ipmovie.c:612
#define OPCODE_CREATE_GRADIENT
Definition: ipmovie.c:67
#define OPCODE_END_OF_STREAM
Definition: ipmovie.c:56
#define OPCODE_SET_DECODING_MAP
Definition: ipmovie.c:71
#define OPCODE_SET_PALETTE_COMPRESSED
Definition: ipmovie.c:69
#define CHUNK_PREAMBLE_SIZE
Definition: ipmovie.c:40
#define OPCODE_START_STOP_AUDIO
Definition: ipmovie.c:60
#define OPCODE_UNKNOWN_14
Definition: ipmovie.c:76
#define OPCODE_AUDIO_FRAME
Definition: ipmovie.c:64
static int ipmovie_read_header(AVFormatContext *s)
Definition: ipmovie.c:627
#define OPCODE_UNKNOWN_15
Definition: ipmovie.c:77
static int ipmovie_probe(const AVProbeData *p)
Definition: ipmovie.c:614
#define OPCODE_UNKNOWN_13
Definition: ipmovie.c:75
#define CHUNK_HAVE_PACKET
Definition: ipmovie.c:50
#define OPCODE_END_OF_CHUNK
Definition: ipmovie.c:57
#define CHUNK_VIDEO
Definition: ipmovie.c:46
#define CHUNK_END
Definition: ipmovie.c:48
#define CHUNK_AUDIO_ONLY
Definition: ipmovie.c:44
static int ipmovie_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ipmovie.c:700
#define CHUNK_EOF
Definition: ipmovie.c:53
#define OPCODE_CREATE_TIMER
Definition: ipmovie.c:58
static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: ipmovie.c:120
#define OPCODE_VIDEO_DATA_10
Definition: ipmovie.c:72
static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb, AVPacket *pkt)
Definition: ipmovie.c:276
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4944
int ff_add_param_change(AVPacket *pkt, int32_t channels, uint64_t channel_layout, int32_t sample_rate, int32_t width, int32_t height)
Add side data to a packet for changing parameters to the given values.
Definition: utils.c:5076
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
int width
Video only.
Definition: codec_par.h:126
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int index
stream index in AVFormatContext
Definition: avformat.h:874
unsigned char * buf
Definition: ipmovie.c:83
int64_t audio_chunk_offset
Definition: ipmovie.c:107
unsigned int audio_bits
Definition: ipmovie.c:98
int audio_chunk_size
Definition: ipmovie.c:108
unsigned int audio_sample_rate
Definition: ipmovie.c:100
unsigned int audio_channels
Definition: ipmovie.c:99
int64_t video_pts
Definition: ipmovie.c:91
uint64_t frame_pts_inc
Definition: ipmovie.c:86
uint8_t send_buffer
Definition: ipmovie.c:95
int64_t video_chunk_offset
Definition: ipmovie.c:109
unsigned int video_height
Definition: ipmovie.c:90
int video_chunk_size
Definition: ipmovie.c:110
int decode_map_chunk_size
Definition: ipmovie.c:114
int skip_map_chunk_size
Definition: ipmovie.c:112
unsigned int video_width
Definition: ipmovie.c:89
int audio_stream_index
Definition: ipmovie.c:105
uint8_t frame_format
Definition: ipmovie.c:96
AVFormatContext * avf
Definition: ipmovie.c:82
int64_t decode_map_chunk_offset
Definition: ipmovie.c:113
int buf_size
Definition: ipmovie.c:84
int64_t skip_map_chunk_offset
Definition: ipmovie.c:111
int64_t next_chunk_offset
Definition: ipmovie.c:116
int changed
Definition: ipmovie.c:94
int has_palette
Definition: ipmovie.c:93
uint32_t palette[256]
Definition: ipmovie.c:92
unsigned int video_bpp
Definition: ipmovie.c:88
enum AVCodecID audio_type
Definition: ipmovie.c:101
unsigned int audio_frame_count
Definition: ipmovie.c:102
int video_stream_index
Definition: ipmovie.c:104
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
if(ret< 0)
Definition: vf_mcdeint.c:282