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