bethsoftvid.c 9.7 KB

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