rl2.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * RL2 Format Demuxer
  3. * Copyright (c) 2008 Sascha Sommer (saschasommer@freenet.de)
  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. * RL2 file demuxer
  23. * @file
  24. * @author Sascha Sommer (saschasommer@freenet.de)
  25. * For more information regarding the RL2 file format, visit:
  26. * http://wiki.multimedia.cx/index.php?title=RL2
  27. *
  28. * extradata:
  29. * 2 byte le initial drawing offset within 320x200 viewport
  30. * 4 byte le number of used colors
  31. * 256 * 3 bytes rgb palette
  32. * optional background_frame
  33. */
  34. #include "libavutil/intreadwrite.h"
  35. #include "avformat.h"
  36. #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
  37. #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
  38. #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
  39. #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
  40. typedef struct Rl2DemuxContext {
  41. unsigned int index_pos[2]; ///< indexes in the sample tables
  42. } Rl2DemuxContext;
  43. /**
  44. * check if the file is in rl2 format
  45. * @param p probe buffer
  46. * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
  47. */
  48. static int rl2_probe(AVProbeData *p)
  49. {
  50. if(AV_RB32(&p->buf[0]) != FORM_TAG)
  51. return 0;
  52. if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
  53. AV_RB32(&p->buf[8]) != RLV3_TAG)
  54. return 0;
  55. return AVPROBE_SCORE_MAX;
  56. }
  57. /**
  58. * read rl2 header data and setup the avstreams
  59. * @param s demuxer context
  60. * @param ap format parameters
  61. * @return 0 on success, AVERROR otherwise
  62. */
  63. static av_cold int rl2_read_header(AVFormatContext *s,
  64. AVFormatParameters *ap)
  65. {
  66. AVIOContext *pb = s->pb;
  67. AVStream *st;
  68. unsigned int frame_count;
  69. unsigned int audio_frame_counter = 0;
  70. unsigned int video_frame_counter = 0;
  71. unsigned int back_size;
  72. unsigned short sound_rate;
  73. unsigned short rate;
  74. unsigned short channels;
  75. unsigned short def_sound_size;
  76. unsigned int signature;
  77. unsigned int pts_den = 11025; /* video only case */
  78. unsigned int pts_num = 1103;
  79. unsigned int* chunk_offset = NULL;
  80. int* chunk_size = NULL;
  81. int* audio_size = NULL;
  82. int i;
  83. int ret = 0;
  84. avio_skip(pb,4); /* skip FORM tag */
  85. back_size = avio_rl32(pb); /**< get size of the background frame */
  86. signature = avio_rb32(pb);
  87. avio_skip(pb, 4); /* data size */
  88. frame_count = avio_rl32(pb);
  89. /* disallow back_sizes and frame_counts that may lead to overflows later */
  90. if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
  91. return AVERROR_INVALIDDATA;
  92. avio_skip(pb, 2); /* encoding mentod */
  93. sound_rate = avio_rl16(pb);
  94. rate = avio_rl16(pb);
  95. channels = avio_rl16(pb);
  96. def_sound_size = avio_rl16(pb);
  97. /** setup video stream */
  98. st = av_new_stream(s, 0);
  99. if(!st)
  100. return AVERROR(ENOMEM);
  101. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  102. st->codec->codec_id = CODEC_ID_RL2;
  103. st->codec->codec_tag = 0; /* no fourcc */
  104. st->codec->width = 320;
  105. st->codec->height = 200;
  106. /** allocate and fill extradata */
  107. st->codec->extradata_size = EXTRADATA1_SIZE;
  108. if(signature == RLV3_TAG && back_size > 0)
  109. st->codec->extradata_size += back_size;
  110. st->codec->extradata = av_mallocz(st->codec->extradata_size +
  111. FF_INPUT_BUFFER_PADDING_SIZE);
  112. if(!st->codec->extradata)
  113. return AVERROR(ENOMEM);
  114. if(avio_read(pb,st->codec->extradata,st->codec->extradata_size) !=
  115. st->codec->extradata_size)
  116. return AVERROR(EIO);
  117. /** setup audio stream if present */
  118. if(sound_rate){
  119. pts_num = def_sound_size;
  120. pts_den = rate;
  121. st = av_new_stream(s, 0);
  122. if (!st)
  123. return AVERROR(ENOMEM);
  124. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  125. st->codec->codec_id = CODEC_ID_PCM_U8;
  126. st->codec->codec_tag = 1;
  127. st->codec->channels = channels;
  128. st->codec->bits_per_coded_sample = 8;
  129. st->codec->sample_rate = rate;
  130. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  131. st->codec->bits_per_coded_sample;
  132. st->codec->block_align = st->codec->channels *
  133. st->codec->bits_per_coded_sample / 8;
  134. av_set_pts_info(st,32,1,rate);
  135. }
  136. av_set_pts_info(s->streams[0], 32, pts_num, pts_den);
  137. chunk_size = av_malloc(frame_count * sizeof(uint32_t));
  138. audio_size = av_malloc(frame_count * sizeof(uint32_t));
  139. chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
  140. if(!chunk_size || !audio_size || !chunk_offset){
  141. av_free(chunk_size);
  142. av_free(audio_size);
  143. av_free(chunk_offset);
  144. return AVERROR(ENOMEM);
  145. }
  146. /** read offset and size tables */
  147. for(i=0; i < frame_count;i++)
  148. chunk_size[i] = avio_rl32(pb);
  149. for(i=0; i < frame_count;i++)
  150. chunk_offset[i] = avio_rl32(pb);
  151. for(i=0; i < frame_count;i++)
  152. audio_size[i] = avio_rl32(pb) & 0xFFFF;
  153. /** build the sample index */
  154. for(i=0;i<frame_count;i++){
  155. if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
  156. ret = AVERROR_INVALIDDATA;
  157. break;
  158. }
  159. if(sound_rate && audio_size[i]){
  160. av_add_index_entry(s->streams[1], chunk_offset[i],
  161. audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
  162. audio_frame_counter += audio_size[i] / channels;
  163. }
  164. av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
  165. video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
  166. ++video_frame_counter;
  167. }
  168. av_free(chunk_size);
  169. av_free(audio_size);
  170. av_free(chunk_offset);
  171. return ret;
  172. }
  173. /**
  174. * read a single audio or video packet
  175. * @param s demuxer context
  176. * @param pkt the packet to be filled
  177. * @return 0 on success, AVERROR otherwise
  178. */
  179. static int rl2_read_packet(AVFormatContext *s,
  180. AVPacket *pkt)
  181. {
  182. Rl2DemuxContext *rl2 = s->priv_data;
  183. AVIOContext *pb = s->pb;
  184. AVIndexEntry *sample = NULL;
  185. int i;
  186. int ret = 0;
  187. int stream_id = -1;
  188. int64_t pos = INT64_MAX;
  189. /** check if there is a valid video or audio entry that can be used */
  190. for(i=0; i<s->nb_streams; i++){
  191. if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
  192. && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
  193. sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
  194. pos= sample->pos;
  195. stream_id= i;
  196. }
  197. }
  198. if(stream_id == -1)
  199. return AVERROR(EIO);
  200. ++rl2->index_pos[stream_id];
  201. /** position the stream (will probably be there anyway) */
  202. avio_seek(pb, sample->pos, SEEK_SET);
  203. /** fill the packet */
  204. ret = av_get_packet(pb, pkt, sample->size);
  205. if(ret != sample->size){
  206. av_free_packet(pkt);
  207. return AVERROR(EIO);
  208. }
  209. pkt->stream_index = stream_id;
  210. pkt->pts = sample->timestamp;
  211. return ret;
  212. }
  213. /**
  214. * seek to a new timestamp
  215. * @param s demuxer context
  216. * @param stream_index index of the stream that should be seeked
  217. * @param timestamp wanted timestamp
  218. * @param flags direction and seeking mode
  219. * @return 0 on success, -1 otherwise
  220. */
  221. static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  222. {
  223. AVStream *st = s->streams[stream_index];
  224. Rl2DemuxContext *rl2 = s->priv_data;
  225. int i;
  226. int index = av_index_search_timestamp(st, timestamp, flags);
  227. if(index < 0)
  228. return -1;
  229. rl2->index_pos[stream_index] = index;
  230. timestamp = st->index_entries[index].timestamp;
  231. for(i=0; i < s->nb_streams; i++){
  232. AVStream *st2 = s->streams[i];
  233. index = av_index_search_timestamp(st2,
  234. av_rescale_q(timestamp, st->time_base, st2->time_base),
  235. flags | AVSEEK_FLAG_BACKWARD);
  236. if(index < 0)
  237. index = 0;
  238. rl2->index_pos[i] = index;
  239. }
  240. return 0;
  241. }
  242. AVInputFormat ff_rl2_demuxer = {
  243. "rl2",
  244. NULL_IF_CONFIG_SMALL("RL2 format"),
  245. sizeof(Rl2DemuxContext),
  246. rl2_probe,
  247. rl2_read_header,
  248. rl2_read_packet,
  249. NULL,
  250. rl2_read_seek,
  251. };