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.

298 lines
9.9KB

  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/channel_layout.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "libavcodec/bethsoftvideo.h"
  33. #define BVID_PALETTE_SIZE 3 * 256
  34. #define DEFAULT_SAMPLE_RATE 11111
  35. typedef struct BVID_DemuxContext
  36. {
  37. int nframes;
  38. int sample_rate; /**< audio sample rate */
  39. int width; /**< video width */
  40. int height; /**< video height */
  41. /** delay value between frames, added to individual frame delay.
  42. * custom units, which will be added to other custom units (~=16ms according
  43. * to free, unofficial documentation) */
  44. int bethsoft_global_delay;
  45. int video_index; /**< video stream index */
  46. int audio_index; /**< audio stream index */
  47. uint8_t *palette;
  48. int is_finished;
  49. } BVID_DemuxContext;
  50. static int vid_probe(AVProbeData *p)
  51. {
  52. // little-endian VID tag, file starts with "VID\0"
  53. if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
  54. return 0;
  55. return AVPROBE_SCORE_MAX;
  56. }
  57. static int vid_read_header(AVFormatContext *s)
  58. {
  59. BVID_DemuxContext *vid = s->priv_data;
  60. AVIOContext *pb = s->pb;
  61. /* load main header. Contents:
  62. * bytes: 'V' 'I' 'D'
  63. * int16s: always_512, nframes, width, height, delay, always_14
  64. */
  65. avio_skip(pb, 5);
  66. vid->nframes = avio_rl16(pb);
  67. vid->width = avio_rl16(pb);
  68. vid->height = avio_rl16(pb);
  69. vid->bethsoft_global_delay = avio_rl16(pb);
  70. avio_rl16(pb);
  71. // wait until the first packet to create each stream
  72. vid->video_index = -1;
  73. vid->audio_index = -1;
  74. vid->sample_rate = DEFAULT_SAMPLE_RATE;
  75. s->ctx_flags |= AVFMTCTX_NOHEADER;
  76. return 0;
  77. }
  78. #define BUFFER_PADDING_SIZE 1000
  79. static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
  80. uint8_t block_type, AVFormatContext *s)
  81. {
  82. uint8_t * vidbuf_start = NULL;
  83. int vidbuf_nbytes = 0;
  84. int code;
  85. int bytes_copied = 0;
  86. int position, duration, npixels;
  87. unsigned int vidbuf_capacity;
  88. int ret = 0;
  89. AVStream *st;
  90. if (vid->video_index < 0) {
  91. st = avformat_new_stream(s, NULL);
  92. if (!st)
  93. return AVERROR(ENOMEM);
  94. vid->video_index = st->index;
  95. if (vid->audio_index < 0) {
  96. avpriv_request_sample(s, "Using default video time base since "
  97. "having no audio packet before the first "
  98. "video packet");
  99. }
  100. avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
  101. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  102. st->codec->codec_id = AV_CODEC_ID_BETHSOFTVID;
  103. st->codec->width = vid->width;
  104. st->codec->height = vid->height;
  105. }
  106. st = s->streams[vid->video_index];
  107. npixels = st->codec->width * st->codec->height;
  108. vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
  109. if(!vidbuf_start)
  110. return AVERROR(ENOMEM);
  111. // save the file position for the packet, include block type
  112. position = avio_tell(pb) - 1;
  113. vidbuf_start[vidbuf_nbytes++] = block_type;
  114. // get the current packet duration
  115. duration = vid->bethsoft_global_delay + avio_rl16(pb);
  116. // set the y offset if it exists (decoder header data should be in data section)
  117. if(block_type == VIDEO_YOFF_P_FRAME){
  118. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
  119. ret = AVERROR(EIO);
  120. goto fail;
  121. }
  122. vidbuf_nbytes += 2;
  123. }
  124. do{
  125. vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
  126. if(!vidbuf_start)
  127. return AVERROR(ENOMEM);
  128. code = avio_r8(pb);
  129. vidbuf_start[vidbuf_nbytes++] = code;
  130. if(code >= 0x80){ // rle sequence
  131. if(block_type == VIDEO_I_FRAME)
  132. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  133. } else if(code){ // plain sequence
  134. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
  135. ret = AVERROR(EIO);
  136. goto fail;
  137. }
  138. vidbuf_nbytes += code;
  139. }
  140. bytes_copied += code & 0x7F;
  141. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  142. // may contain a 0 byte even if read all pixels
  143. if(avio_r8(pb))
  144. avio_seek(pb, -1, SEEK_CUR);
  145. break;
  146. }
  147. if (bytes_copied > npixels) {
  148. ret = AVERROR_INVALIDDATA;
  149. goto fail;
  150. }
  151. } while(code);
  152. // copy data into packet
  153. if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
  154. goto fail;
  155. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  156. pkt->pos = position;
  157. pkt->stream_index = vid->video_index;
  158. pkt->duration = duration;
  159. if (block_type == VIDEO_I_FRAME)
  160. pkt->flags |= AV_PKT_FLAG_KEY;
  161. /* if there is a new palette available, add it to packet side data */
  162. if (vid->palette) {
  163. uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  164. BVID_PALETTE_SIZE);
  165. if (!pdata) {
  166. ret = AVERROR(ENOMEM);
  167. goto fail;
  168. }
  169. memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
  170. av_freep(&vid->palette);
  171. }
  172. vid->nframes--; // used to check if all the frames were read
  173. fail:
  174. av_free(vidbuf_start);
  175. return ret;
  176. }
  177. static int vid_read_packet(AVFormatContext *s,
  178. AVPacket *pkt)
  179. {
  180. BVID_DemuxContext *vid = s->priv_data;
  181. AVIOContext *pb = s->pb;
  182. unsigned char block_type;
  183. int audio_length;
  184. int ret_value;
  185. if(vid->is_finished || pb->eof_reached)
  186. return AVERROR(EIO);
  187. block_type = avio_r8(pb);
  188. switch(block_type){
  189. case PALETTE_BLOCK:
  190. if (vid->palette) {
  191. av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
  192. av_freep(&vid->palette);
  193. }
  194. vid->palette = av_malloc(BVID_PALETTE_SIZE);
  195. if (!vid->palette)
  196. return AVERROR(ENOMEM);
  197. if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
  198. av_freep(&vid->palette);
  199. return AVERROR(EIO);
  200. }
  201. return vid_read_packet(s, pkt);
  202. case FIRST_AUDIO_BLOCK:
  203. avio_rl16(pb);
  204. // soundblaster DAC used for sample rate, as on specification page (link above)
  205. vid->sample_rate = 1000000 / (256 - avio_r8(pb));
  206. case AUDIO_BLOCK:
  207. if (vid->audio_index < 0) {
  208. AVStream *st = avformat_new_stream(s, NULL);
  209. if (!st)
  210. return AVERROR(ENOMEM);
  211. vid->audio_index = st->index;
  212. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  213. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  214. st->codec->channels = 1;
  215. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  216. st->codec->bits_per_coded_sample = 8;
  217. st->codec->sample_rate = vid->sample_rate;
  218. st->codec->bit_rate = 8 * st->codec->sample_rate;
  219. st->start_time = 0;
  220. avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
  221. }
  222. audio_length = avio_rl16(pb);
  223. if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
  224. if (ret_value < 0)
  225. return ret_value;
  226. av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
  227. return AVERROR(EIO);
  228. }
  229. pkt->stream_index = vid->audio_index;
  230. pkt->duration = audio_length;
  231. pkt->flags |= AV_PKT_FLAG_KEY;
  232. return 0;
  233. case VIDEO_P_FRAME:
  234. case VIDEO_YOFF_P_FRAME:
  235. case VIDEO_I_FRAME:
  236. return read_frame(vid, pb, pkt, block_type, s);
  237. case EOF_BLOCK:
  238. if(vid->nframes != 0)
  239. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  240. vid->is_finished = 1;
  241. return AVERROR(EIO);
  242. default:
  243. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  244. block_type, block_type, block_type);
  245. return AVERROR_INVALIDDATA;
  246. }
  247. }
  248. static int vid_read_close(AVFormatContext *s)
  249. {
  250. BVID_DemuxContext *vid = s->priv_data;
  251. av_freep(&vid->palette);
  252. return 0;
  253. }
  254. AVInputFormat ff_bethsoftvid_demuxer = {
  255. .name = "bethsoftvid",
  256. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
  257. .priv_data_size = sizeof(BVID_DemuxContext),
  258. .read_probe = vid_probe,
  259. .read_header = vid_read_header,
  260. .read_packet = vid_read_packet,
  261. .read_close = vid_read_close,
  262. };