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.

234 lines
7.8KB

  1. /*
  2. * Bethsoft VID format Demuxer
  3. * Copyright (c) 2007 Nicholas Tung
  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. * @file
  23. * @brief Bethesda Softworks VID (.vid) file demuxer
  24. * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
  25. * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
  26. * @see http://www.svatopluk.com/andux/docs/dfvid.html
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. #include "libavcodec/bethsoftvideo.h"
  32. typedef struct BVID_DemuxContext
  33. {
  34. int nframes;
  35. /** delay value between frames, added to individual frame delay.
  36. * custom units, which will be added to other custom units (~=16ms according
  37. * to free, unofficial documentation) */
  38. int bethsoft_global_delay;
  39. /** video presentation time stamp.
  40. * delay = 16 milliseconds * (global_delay + per_frame_delay) */
  41. int video_pts;
  42. int is_finished;
  43. } BVID_DemuxContext;
  44. static int vid_probe(AVProbeData *p)
  45. {
  46. // little endian VID tag, file starts with "VID\0"
  47. if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
  48. return 0;
  49. return AVPROBE_SCORE_MAX;
  50. }
  51. static int vid_read_header(AVFormatContext *s,
  52. AVFormatParameters *ap)
  53. {
  54. BVID_DemuxContext *vid = s->priv_data;
  55. AVIOContext *pb = s->pb;
  56. AVStream *stream;
  57. /* load main header. Contents:
  58. * bytes: 'V' 'I' 'D'
  59. * int16s: always_512, nframes, width, height, delay, always_14
  60. */
  61. avio_skip(pb, 5);
  62. vid->nframes = avio_rl16(pb);
  63. stream = avformat_new_stream(s, NULL);
  64. if (!stream)
  65. return AVERROR(ENOMEM);
  66. avpriv_set_pts_info(stream, 32, 1, 60); // 16 ms increments, i.e. 60 fps
  67. stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  68. stream->codec->codec_id = CODEC_ID_BETHSOFTVID;
  69. stream->codec->width = avio_rl16(pb);
  70. stream->codec->height = avio_rl16(pb);
  71. stream->codec->pix_fmt = PIX_FMT_PAL8;
  72. vid->bethsoft_global_delay = avio_rl16(pb);
  73. avio_rl16(pb);
  74. // done with video codec, set up audio codec
  75. stream = avformat_new_stream(s, NULL);
  76. if (!stream)
  77. return AVERROR(ENOMEM);
  78. stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  79. stream->codec->codec_id = CODEC_ID_PCM_U8;
  80. stream->codec->channels = 1;
  81. stream->codec->sample_rate = 11025;
  82. stream->codec->bits_per_coded_sample = 8;
  83. stream->codec->bit_rate = stream->codec->channels * stream->codec->sample_rate * stream->codec->bits_per_coded_sample;
  84. return 0;
  85. }
  86. #define BUFFER_PADDING_SIZE 1000
  87. static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
  88. uint8_t block_type, AVFormatContext *s, int npixels)
  89. {
  90. uint8_t * vidbuf_start = NULL;
  91. int vidbuf_nbytes = 0;
  92. int code;
  93. int bytes_copied = 0;
  94. int position;
  95. unsigned int vidbuf_capacity;
  96. vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
  97. if(!vidbuf_start)
  98. return AVERROR(ENOMEM);
  99. // save the file position for the packet, include block type
  100. position = avio_tell(pb) - 1;
  101. vidbuf_start[vidbuf_nbytes++] = block_type;
  102. // get the video delay (next int16), and set the presentation time
  103. vid->video_pts += vid->bethsoft_global_delay + avio_rl16(pb);
  104. // set the y offset if it exists (decoder header data should be in data section)
  105. if(block_type == VIDEO_YOFF_P_FRAME){
  106. if(avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2)
  107. goto fail;
  108. vidbuf_nbytes += 2;
  109. }
  110. do{
  111. vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
  112. if(!vidbuf_start)
  113. return AVERROR(ENOMEM);
  114. code = avio_r8(pb);
  115. vidbuf_start[vidbuf_nbytes++] = code;
  116. if(code >= 0x80){ // rle sequence
  117. if(block_type == VIDEO_I_FRAME)
  118. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  119. } else if(code){ // plain sequence
  120. if(avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code)
  121. goto fail;
  122. vidbuf_nbytes += code;
  123. }
  124. bytes_copied += code & 0x7F;
  125. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  126. // may contain a 0 byte even if read all pixels
  127. if(avio_r8(pb))
  128. avio_seek(pb, -1, SEEK_CUR);
  129. break;
  130. }
  131. if(bytes_copied > npixels)
  132. goto fail;
  133. } while(code);
  134. // copy data into packet
  135. if(av_new_packet(pkt, vidbuf_nbytes) < 0)
  136. goto fail;
  137. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  138. av_free(vidbuf_start);
  139. pkt->pos = position;
  140. pkt->stream_index = 0; // use the video decoder, which was initialized as the first stream
  141. pkt->pts = vid->video_pts;
  142. vid->nframes--; // used to check if all the frames were read
  143. return vidbuf_nbytes;
  144. fail:
  145. av_free(vidbuf_start);
  146. return -1;
  147. }
  148. static int vid_read_packet(AVFormatContext *s,
  149. AVPacket *pkt)
  150. {
  151. BVID_DemuxContext *vid = s->priv_data;
  152. AVIOContext *pb = s->pb;
  153. unsigned char block_type;
  154. int audio_length;
  155. int ret_value;
  156. if(vid->is_finished || pb->eof_reached)
  157. return AVERROR(EIO);
  158. block_type = avio_r8(pb);
  159. switch(block_type){
  160. case PALETTE_BLOCK:
  161. avio_seek(pb, -1, SEEK_CUR); // include block type
  162. ret_value = av_get_packet(pb, pkt, 3 * 256 + 1);
  163. if(ret_value != 3 * 256 + 1){
  164. av_free_packet(pkt);
  165. return AVERROR(EIO);
  166. }
  167. pkt->stream_index = 0;
  168. return ret_value;
  169. case FIRST_AUDIO_BLOCK:
  170. avio_rl16(pb);
  171. // soundblaster DAC used for sample rate, as on specification page (link above)
  172. s->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));
  173. s->streams[1]->codec->bit_rate = s->streams[1]->codec->channels * s->streams[1]->codec->sample_rate * s->streams[1]->codec->bits_per_coded_sample;
  174. case AUDIO_BLOCK:
  175. audio_length = avio_rl16(pb);
  176. ret_value = av_get_packet(pb, pkt, audio_length);
  177. pkt->stream_index = 1;
  178. return ret_value != audio_length ? AVERROR(EIO) : ret_value;
  179. case VIDEO_P_FRAME:
  180. case VIDEO_YOFF_P_FRAME:
  181. case VIDEO_I_FRAME:
  182. return read_frame(vid, pb, pkt, block_type, s,
  183. s->streams[0]->codec->width * s->streams[0]->codec->height);
  184. case EOF_BLOCK:
  185. if(vid->nframes != 0)
  186. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  187. vid->is_finished = 1;
  188. return AVERROR(EIO);
  189. default:
  190. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  191. block_type, block_type, block_type); return -1;
  192. }
  193. }
  194. AVInputFormat ff_bethsoftvid_demuxer = {
  195. .name = "bethsoftvid",
  196. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID format"),
  197. .priv_data_size = sizeof(BVID_DemuxContext),
  198. .read_probe = vid_probe,
  199. .read_header = vid_read_header,
  200. .read_packet = vid_read_packet,
  201. };