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.

303 lines
10KB

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