rl2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * @param ap format parameters
  62. * @return 0 on success, AVERROR otherwise
  63. */
  64. static av_cold int rl2_read_header(AVFormatContext *s,
  65. AVFormatParameters *ap)
  66. {
  67. AVIOContext *pb = s->pb;
  68. AVStream *st;
  69. unsigned int frame_count;
  70. unsigned int audio_frame_counter = 0;
  71. unsigned int video_frame_counter = 0;
  72. unsigned int back_size;
  73. unsigned short sound_rate;
  74. unsigned short rate;
  75. unsigned short channels;
  76. unsigned short def_sound_size;
  77. unsigned int signature;
  78. unsigned int pts_den = 11025; /* video only case */
  79. unsigned int pts_num = 1103;
  80. unsigned int* chunk_offset = NULL;
  81. int* chunk_size = NULL;
  82. int* audio_size = NULL;
  83. int i;
  84. int ret = 0;
  85. avio_skip(pb,4); /* skip FORM tag */
  86. back_size = avio_rl32(pb); /**< get size of the background frame */
  87. signature = avio_rb32(pb);
  88. avio_skip(pb, 4); /* data size */
  89. frame_count = avio_rl32(pb);
  90. /* disallow back_sizes and frame_counts that may lead to overflows later */
  91. if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
  92. return AVERROR_INVALIDDATA;
  93. avio_skip(pb, 2); /* encoding mentod */
  94. sound_rate = avio_rl16(pb);
  95. rate = avio_rl16(pb);
  96. channels = avio_rl16(pb);
  97. def_sound_size = avio_rl16(pb);
  98. if (!channels || channels > 42) {
  99. av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
  100. return AVERROR_INVALIDDATA;
  101. }
  102. /** setup video stream */
  103. st = avformat_new_stream(s, NULL);
  104. if(!st)
  105. return AVERROR(ENOMEM);
  106. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  107. st->codec->codec_id = CODEC_ID_RL2;
  108. st->codec->codec_tag = 0; /* no fourcc */
  109. st->codec->width = 320;
  110. st->codec->height = 200;
  111. /** allocate and fill extradata */
  112. st->codec->extradata_size = EXTRADATA1_SIZE;
  113. if(signature == RLV3_TAG && back_size > 0)
  114. st->codec->extradata_size += back_size;
  115. st->codec->extradata = av_mallocz(st->codec->extradata_size +
  116. FF_INPUT_BUFFER_PADDING_SIZE);
  117. if(!st->codec->extradata)
  118. return AVERROR(ENOMEM);
  119. if(avio_read(pb,st->codec->extradata,st->codec->extradata_size) !=
  120. st->codec->extradata_size)
  121. return AVERROR(EIO);
  122. /** setup audio stream if present */
  123. if(sound_rate){
  124. if(channels <= 0)
  125. return AVERROR_INVALIDDATA;
  126. pts_num = def_sound_size;
  127. pts_den = rate;
  128. st = avformat_new_stream(s, NULL);
  129. if (!st)
  130. return AVERROR(ENOMEM);
  131. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  132. st->codec->codec_id = CODEC_ID_PCM_U8;
  133. st->codec->codec_tag = 1;
  134. st->codec->channels = channels;
  135. st->codec->bits_per_coded_sample = 8;
  136. st->codec->sample_rate = rate;
  137. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  138. st->codec->bits_per_coded_sample;
  139. st->codec->block_align = st->codec->channels *
  140. st->codec->bits_per_coded_sample / 8;
  141. avpriv_set_pts_info(st,32,1,rate);
  142. }
  143. avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
  144. chunk_size = av_malloc(frame_count * sizeof(uint32_t));
  145. audio_size = av_malloc(frame_count * sizeof(uint32_t));
  146. chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
  147. if(!chunk_size || !audio_size || !chunk_offset){
  148. av_free(chunk_size);
  149. av_free(audio_size);
  150. av_free(chunk_offset);
  151. return AVERROR(ENOMEM);
  152. }
  153. /** read offset and size tables */
  154. for(i=0; i < frame_count;i++)
  155. chunk_size[i] = avio_rl32(pb);
  156. for(i=0; i < frame_count;i++)
  157. chunk_offset[i] = avio_rl32(pb);
  158. for(i=0; i < frame_count;i++)
  159. audio_size[i] = avio_rl32(pb) & 0xFFFF;
  160. /** build the sample index */
  161. for(i=0;i<frame_count;i++){
  162. if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
  163. ret = AVERROR_INVALIDDATA;
  164. break;
  165. }
  166. if(sound_rate && audio_size[i]){
  167. av_add_index_entry(s->streams[1], chunk_offset[i],
  168. audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
  169. audio_frame_counter += audio_size[i] / channels;
  170. }
  171. av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
  172. video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
  173. ++video_frame_counter;
  174. }
  175. av_free(chunk_size);
  176. av_free(audio_size);
  177. av_free(chunk_offset);
  178. return ret;
  179. }
  180. /**
  181. * read a single audio or video packet
  182. * @param s demuxer context
  183. * @param pkt the packet to be filled
  184. * @return 0 on success, AVERROR otherwise
  185. */
  186. static int rl2_read_packet(AVFormatContext *s,
  187. AVPacket *pkt)
  188. {
  189. Rl2DemuxContext *rl2 = s->priv_data;
  190. AVIOContext *pb = s->pb;
  191. AVIndexEntry *sample = NULL;
  192. int i;
  193. int ret = 0;
  194. int stream_id = -1;
  195. int64_t pos = INT64_MAX;
  196. /** check if there is a valid video or audio entry that can be used */
  197. for(i=0; i<s->nb_streams; i++){
  198. if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
  199. && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
  200. sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
  201. pos= sample->pos;
  202. stream_id= i;
  203. }
  204. }
  205. if(stream_id == -1)
  206. return AVERROR(EIO);
  207. ++rl2->index_pos[stream_id];
  208. /** position the stream (will probably be there anyway) */
  209. avio_seek(pb, sample->pos, SEEK_SET);
  210. /** fill the packet */
  211. ret = av_get_packet(pb, pkt, sample->size);
  212. if(ret != sample->size){
  213. av_free_packet(pkt);
  214. return AVERROR(EIO);
  215. }
  216. pkt->stream_index = stream_id;
  217. pkt->pts = sample->timestamp;
  218. return ret;
  219. }
  220. /**
  221. * seek to a new timestamp
  222. * @param s demuxer context
  223. * @param stream_index index of the stream that should be seeked
  224. * @param timestamp wanted timestamp
  225. * @param flags direction and seeking mode
  226. * @return 0 on success, -1 otherwise
  227. */
  228. static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  229. {
  230. AVStream *st = s->streams[stream_index];
  231. Rl2DemuxContext *rl2 = s->priv_data;
  232. int i;
  233. int index = av_index_search_timestamp(st, timestamp, flags);
  234. if(index < 0)
  235. return -1;
  236. rl2->index_pos[stream_index] = index;
  237. timestamp = st->index_entries[index].timestamp;
  238. for(i=0; i < s->nb_streams; i++){
  239. AVStream *st2 = s->streams[i];
  240. index = av_index_search_timestamp(st2,
  241. av_rescale_q(timestamp, st->time_base, st2->time_base),
  242. flags | AVSEEK_FLAG_BACKWARD);
  243. if(index < 0)
  244. index = 0;
  245. rl2->index_pos[i] = index;
  246. }
  247. return 0;
  248. }
  249. AVInputFormat ff_rl2_demuxer = {
  250. .name = "rl2",
  251. .long_name = NULL_IF_CONFIG_SMALL("RL2 format"),
  252. .priv_data_size = sizeof(Rl2DemuxContext),
  253. .read_probe = rl2_probe,
  254. .read_header = rl2_read_header,
  255. .read_packet = rl2_read_packet,
  256. .read_seek = rl2_read_seek,
  257. };