You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
11KB

  1. /*
  2. * Sierra VMD Format Demuxer
  3. * Copyright (c) 2004 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file sierravmd.c
  21. * Sierra VMD file demuxer
  22. * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
  23. * for more information on the Sierra VMD file format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. */
  26. #include "avformat.h"
  27. #define VMD_HEADER_SIZE 0x0330
  28. #define BYTES_PER_FRAME_RECORD 16
  29. typedef struct {
  30. int stream_index;
  31. offset_t frame_offset;
  32. unsigned int frame_size;
  33. int64_t pts;
  34. int keyframe;
  35. unsigned char frame_record[BYTES_PER_FRAME_RECORD];
  36. } vmd_frame_t;
  37. typedef struct VmdDemuxContext {
  38. int video_stream_index;
  39. int audio_stream_index;
  40. unsigned int audio_type;
  41. unsigned int audio_samplerate;
  42. unsigned int audio_bits;
  43. unsigned int audio_channels;
  44. unsigned int frame_count;
  45. unsigned int frames_per_block;
  46. vmd_frame_t *frame_table;
  47. unsigned int current_frame;
  48. int sample_rate;
  49. int64_t audio_sample_counter;
  50. int audio_frame_divisor;
  51. int audio_block_align;
  52. int skiphdr;
  53. unsigned char vmd_header[VMD_HEADER_SIZE];
  54. } VmdDemuxContext;
  55. static int vmd_probe(AVProbeData *p)
  56. {
  57. if (p->buf_size < 2)
  58. return 0;
  59. /* check if the first 2 bytes of the file contain the appropriate size
  60. * of a VMD header chunk */
  61. if (LE_16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
  62. return 0;
  63. /* only return half certainty since this check is a bit sketchy */
  64. return AVPROBE_SCORE_MAX / 2;
  65. }
  66. /* This is a support function to determine the duration, in sample
  67. * frames, of a particular audio chunk, taking into account silent
  68. * encodings. */
  69. static int vmd_calculate_audio_duration(unsigned char *audio_chunk,
  70. int audio_chunk_size, int block_align)
  71. {
  72. unsigned char *p = audio_chunk + 16;
  73. unsigned char *p_end = audio_chunk + audio_chunk_size;
  74. int total_samples = 0;
  75. unsigned int sound_flags;
  76. if (audio_chunk_size < 16)
  77. return 0;
  78. if (audio_chunk_size == block_align + 16)
  79. return block_align;
  80. if (audio_chunk_size == block_align + 17)
  81. return block_align;
  82. sound_flags = LE_32(p);
  83. p += 4;
  84. while (p < p_end) {
  85. total_samples += block_align;
  86. if ((sound_flags & 0x01) == 0)
  87. p += block_align;
  88. sound_flags >>= 1;
  89. }
  90. av_log(NULL,0,"Got %i samples for size %i map %08X\n", total_samples, audio_chunk_size, LE_32(audio_chunk));
  91. return total_samples;
  92. }
  93. static int vmd_read_header(AVFormatContext *s,
  94. AVFormatParameters *ap)
  95. {
  96. VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
  97. ByteIOContext *pb = &s->pb;
  98. AVStream *st;
  99. unsigned int toc_offset;
  100. unsigned char *raw_frame_table;
  101. int raw_frame_table_size;
  102. offset_t current_offset;
  103. int i, j;
  104. unsigned int total_frames;
  105. int64_t video_pts_inc = 0;
  106. int64_t current_video_pts = 0;
  107. unsigned char chunk[BYTES_PER_FRAME_RECORD];
  108. int lastframe = 0;
  109. /* fetch the main header, including the 2 header length bytes */
  110. url_fseek(pb, 0, SEEK_SET);
  111. if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
  112. return AVERROR_IO;
  113. vmd->audio_sample_counter = 0;
  114. vmd->audio_frame_divisor = 1;
  115. vmd->audio_block_align = 1;
  116. /* start up the decoders */
  117. st = av_new_stream(s, 0);
  118. if (!st)
  119. return AVERROR_NOMEM;
  120. av_set_pts_info(st, 33, 1, 90000);
  121. vmd->video_stream_index = st->index;
  122. st->codec->codec_type = CODEC_TYPE_VIDEO;
  123. st->codec->codec_id = CODEC_ID_VMDVIDEO;
  124. st->codec->codec_tag = 0; /* no fourcc */
  125. st->codec->width = LE_16(&vmd->vmd_header[12]);
  126. st->codec->height = LE_16(&vmd->vmd_header[14]);
  127. st->codec->time_base.num = 1;
  128. st->codec->time_base.den = 10;
  129. st->codec->extradata_size = VMD_HEADER_SIZE;
  130. st->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  131. memcpy(st->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
  132. /* if sample rate is 0, assume no audio */
  133. vmd->sample_rate = LE_16(&vmd->vmd_header[804]);
  134. if (vmd->sample_rate) {
  135. st = av_new_stream(s, 0);
  136. if (!st)
  137. return AVERROR_NOMEM;
  138. av_set_pts_info(st, 33, 1, 90000);
  139. vmd->audio_stream_index = st->index;
  140. st->codec->codec_type = CODEC_TYPE_AUDIO;
  141. st->codec->codec_id = CODEC_ID_VMDAUDIO;
  142. st->codec->codec_tag = 0; /* no fourcc */
  143. st->codec->channels = vmd->audio_channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
  144. st->codec->sample_rate = vmd->sample_rate;
  145. st->codec->block_align = vmd->audio_block_align =
  146. LE_16(&vmd->vmd_header[806]);
  147. if (st->codec->block_align & 0x8000) {
  148. st->codec->bits_per_sample = 16;
  149. st->codec->block_align = -(st->codec->block_align - 0x10000);
  150. vmd->audio_block_align = -(vmd->audio_block_align - 0x10000);
  151. } else {
  152. st->codec->bits_per_sample = 8;
  153. }
  154. st->codec->bit_rate = st->codec->sample_rate *
  155. st->codec->bits_per_sample * st->codec->channels;
  156. /* for calculating pts */
  157. vmd->audio_frame_divisor = st->codec->channels;
  158. video_pts_inc = 90000;
  159. video_pts_inc *= st->codec->block_align;
  160. video_pts_inc /= st->codec->sample_rate;
  161. video_pts_inc /= st->codec->channels;
  162. } else {
  163. /* if no audio, assume 10 frames/second */
  164. video_pts_inc = 90000 / 10;
  165. }
  166. toc_offset = LE_32(&vmd->vmd_header[812]);
  167. vmd->frame_count = LE_16(&vmd->vmd_header[6]);
  168. vmd->frames_per_block = LE_16(&vmd->vmd_header[18]);
  169. url_fseek(pb, toc_offset, SEEK_SET);
  170. raw_frame_table = NULL;
  171. vmd->frame_table = NULL;
  172. raw_frame_table_size = vmd->frame_count * 6;
  173. raw_frame_table = av_malloc(raw_frame_table_size);
  174. vmd->frame_table = av_malloc(vmd->frame_count * vmd->frames_per_block * sizeof(vmd_frame_t));
  175. if (!raw_frame_table || !vmd->frame_table) {
  176. av_free(raw_frame_table);
  177. av_free(vmd->frame_table);
  178. return AVERROR_NOMEM;
  179. }
  180. if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
  181. raw_frame_table_size) {
  182. av_free(raw_frame_table);
  183. av_free(vmd->frame_table);
  184. return AVERROR_IO;
  185. }
  186. total_frames = 0;
  187. for (i = 0; i < vmd->frame_count; i++) {
  188. current_offset = LE_32(&raw_frame_table[6 * i + 2]);
  189. /* handle each entry in index block */
  190. for (j = 0; j < vmd->frames_per_block; j++) {
  191. int type;
  192. uint32_t size;
  193. get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
  194. type = chunk[0];
  195. size = LE_32(&chunk[2]);
  196. if(!size)
  197. continue;
  198. switch(type) {
  199. case 1: /* Audio Chunk */
  200. vmd->frame_table[total_frames].frame_offset = current_offset;
  201. vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
  202. vmd->frame_table[total_frames].frame_size = size;
  203. memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
  204. total_frames++;
  205. break;
  206. case 2: /* Video Chunk */
  207. vmd->frame_table[total_frames].frame_offset = current_offset;
  208. vmd->frame_table[total_frames].frame_size = size;
  209. vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
  210. memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
  211. vmd->frame_table[total_frames].pts = current_video_pts;
  212. if (lastframe) {
  213. vmd->frame_table[lastframe].pts = current_video_pts - video_pts_inc;
  214. }
  215. lastframe = total_frames;
  216. total_frames++;
  217. break;
  218. }
  219. current_offset += size;
  220. }
  221. current_video_pts += video_pts_inc;
  222. }
  223. av_free(raw_frame_table);
  224. vmd->current_frame = 0;
  225. vmd->frame_count = total_frames;
  226. return 0;
  227. }
  228. static int vmd_read_packet(AVFormatContext *s,
  229. AVPacket *pkt)
  230. {
  231. VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
  232. ByteIOContext *pb = &s->pb;
  233. int ret = 0;
  234. vmd_frame_t *frame;
  235. if (vmd->current_frame >= vmd->frame_count)
  236. return AVERROR_IO;
  237. frame = &vmd->frame_table[vmd->current_frame];
  238. /* position the stream (will probably be there already) */
  239. url_fseek(pb, frame->frame_offset, SEEK_SET);
  240. if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
  241. return AVERROR_NOMEM;
  242. pkt->pos= url_ftell(pb);
  243. memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
  244. ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
  245. frame->frame_size);
  246. if (ret != frame->frame_size) {
  247. av_free_packet(pkt);
  248. ret = AVERROR_IO;
  249. }
  250. pkt->stream_index = frame->stream_index;
  251. if (frame->frame_record[0] == 0x02)
  252. pkt->pts = frame->pts;
  253. else {
  254. pkt->pts = vmd->audio_sample_counter;
  255. pkt->pts *= 90000;
  256. pkt->pts /= vmd->sample_rate;
  257. pkt->pts /= vmd->audio_channels;
  258. vmd->audio_sample_counter += vmd_calculate_audio_duration(
  259. pkt->data, pkt->size, vmd->audio_block_align);
  260. }
  261. av_log(NULL, AV_LOG_INFO, " dispatching %s frame with %d bytes and pts %"PRId64" (%0.1f sec)\n",
  262. (frame->frame_record[0] == 0x02) ? "video" : "audio",
  263. frame->frame_size + BYTES_PER_FRAME_RECORD,
  264. pkt->pts, (float)(pkt->pts / 90000.0));
  265. vmd->current_frame++;
  266. return ret;
  267. }
  268. static int vmd_read_close(AVFormatContext *s)
  269. {
  270. VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
  271. av_free(vmd->frame_table);
  272. return 0;
  273. }
  274. static AVInputFormat vmd_iformat = {
  275. "vmd",
  276. "Sierra VMD format",
  277. sizeof(VmdDemuxContext),
  278. vmd_probe,
  279. vmd_read_header,
  280. vmd_read_packet,
  281. vmd_read_close,
  282. };
  283. int vmd_init(void)
  284. {
  285. av_register_input_format(&vmd_iformat);
  286. return 0;
  287. }