PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ffmpeg/libavformat/bethsoftvid.c

http://github.com/xbmc/xbmc
C | 295 lines | 217 code | 35 blank | 43 comment | 40 complexity | dad1a664f38632a4fc4a1550994552d4 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  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. 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. av_log_ask_for_sample(s, "No audio packet before first video "
  97. "packet. Using default video time base.\n");
  98. }
  99. avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
  100. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  101. st->codec->codec_id = AV_CODEC_ID_BETHSOFTVID;
  102. st->codec->width = vid->width;
  103. st->codec->height = vid->height;
  104. }
  105. st = s->streams[vid->video_index];
  106. npixels = st->codec->width * st->codec->height;
  107. vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
  108. if(!vidbuf_start)
  109. return AVERROR(ENOMEM);
  110. // save the file position for the packet, include block type
  111. position = avio_tell(pb) - 1;
  112. vidbuf_start[vidbuf_nbytes++] = block_type;
  113. // get the current packet duration
  114. duration = vid->bethsoft_global_delay + avio_rl16(pb);
  115. // set the y offset if it exists (decoder header data should be in data section)
  116. if(block_type == VIDEO_YOFF_P_FRAME){
  117. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
  118. ret = AVERROR(EIO);
  119. goto fail;
  120. }
  121. vidbuf_nbytes += 2;
  122. }
  123. do{
  124. vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
  125. if(!vidbuf_start)
  126. return AVERROR(ENOMEM);
  127. code = avio_r8(pb);
  128. vidbuf_start[vidbuf_nbytes++] = code;
  129. if(code >= 0x80){ // rle sequence
  130. if(block_type == VIDEO_I_FRAME)
  131. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  132. } else if(code){ // plain sequence
  133. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
  134. ret = AVERROR(EIO);
  135. goto fail;
  136. }
  137. vidbuf_nbytes += code;
  138. }
  139. bytes_copied += code & 0x7F;
  140. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  141. // may contain a 0 byte even if read all pixels
  142. if(avio_r8(pb))
  143. avio_seek(pb, -1, SEEK_CUR);
  144. break;
  145. }
  146. if (bytes_copied > npixels) {
  147. ret = AVERROR_INVALIDDATA;
  148. goto fail;
  149. }
  150. } while(code);
  151. // copy data into packet
  152. if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
  153. goto fail;
  154. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  155. av_free(vidbuf_start);
  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. memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
  167. av_freep(&vid->palette);
  168. }
  169. vid->nframes--; // used to check if all the frames were read
  170. return 0;
  171. fail:
  172. av_free(vidbuf_start);
  173. return ret;
  174. }
  175. static int vid_read_packet(AVFormatContext *s,
  176. AVPacket *pkt)
  177. {
  178. BVID_DemuxContext *vid = s->priv_data;
  179. AVIOContext *pb = s->pb;
  180. unsigned char block_type;
  181. int audio_length;
  182. int ret_value;
  183. if(vid->is_finished || url_feof(pb))
  184. return AVERROR_EOF;
  185. block_type = avio_r8(pb);
  186. switch(block_type){
  187. case PALETTE_BLOCK:
  188. if (vid->palette) {
  189. av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
  190. av_freep(&vid->palette);
  191. }
  192. vid->palette = av_malloc(BVID_PALETTE_SIZE);
  193. if (!vid->palette)
  194. return AVERROR(ENOMEM);
  195. if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
  196. av_freep(&vid->palette);
  197. return AVERROR(EIO);
  198. }
  199. return vid_read_packet(s, pkt);
  200. case FIRST_AUDIO_BLOCK:
  201. avio_rl16(pb);
  202. // soundblaster DAC used for sample rate, as on specification page (link above)
  203. vid->sample_rate = 1000000 / (256 - avio_r8(pb));
  204. case AUDIO_BLOCK:
  205. if (vid->audio_index < 0) {
  206. AVStream *st = avformat_new_stream(s, NULL);
  207. if (!st)
  208. return AVERROR(ENOMEM);
  209. vid->audio_index = st->index;
  210. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  211. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  212. st->codec->channels = 1;
  213. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  214. st->codec->bits_per_coded_sample = 8;
  215. st->codec->sample_rate = vid->sample_rate;
  216. st->codec->bit_rate = 8 * st->codec->sample_rate;
  217. st->start_time = 0;
  218. avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
  219. }
  220. audio_length = avio_rl16(pb);
  221. if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
  222. if (ret_value < 0)
  223. return ret_value;
  224. av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
  225. return AVERROR(EIO);
  226. }
  227. pkt->stream_index = vid->audio_index;
  228. pkt->duration = audio_length;
  229. pkt->flags |= AV_PKT_FLAG_KEY;
  230. return 0;
  231. case VIDEO_P_FRAME:
  232. case VIDEO_YOFF_P_FRAME:
  233. case VIDEO_I_FRAME:
  234. return read_frame(vid, pb, pkt, block_type, s);
  235. case EOF_BLOCK:
  236. if(vid->nframes != 0)
  237. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  238. vid->is_finished = 1;
  239. return AVERROR(EIO);
  240. default:
  241. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  242. block_type, block_type, block_type);
  243. return AVERROR_INVALIDDATA;
  244. }
  245. }
  246. static int vid_read_close(AVFormatContext *s)
  247. {
  248. BVID_DemuxContext *vid = s->priv_data;
  249. av_freep(&vid->palette);
  250. return 0;
  251. }
  252. AVInputFormat ff_bethsoftvid_demuxer = {
  253. .name = "bethsoftvid",
  254. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
  255. .priv_data_size = sizeof(BVID_DemuxContext),
  256. .read_probe = vid_probe,
  257. .read_header = vid_read_header,
  258. .read_packet = vid_read_packet,
  259. .read_close = vid_read_close,
  260. };