You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
9.0KB

  1. /*
  2. * RL2 Format Demuxer
  3. * Copyright (c) 2008 Sascha Sommer (saschasommer@freenet.de)
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 <stdint.h>
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/mathematics.h"
  36. #include "avformat.h"
  37. #include "internal.h"
  38. #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
  39. #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
  40. #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
  41. #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
  42. typedef struct Rl2DemuxContext {
  43. unsigned int index_pos[2]; ///< indexes in the sample tables
  44. } Rl2DemuxContext;
  45. /**
  46. * check if the file is in rl2 format
  47. * @param p probe buffer
  48. * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
  49. */
  50. static int rl2_probe(AVProbeData *p)
  51. {
  52. if(AV_RB32(&p->buf[0]) != FORM_TAG)
  53. return 0;
  54. if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
  55. AV_RB32(&p->buf[8]) != RLV3_TAG)
  56. return 0;
  57. return AVPROBE_SCORE_MAX;
  58. }
  59. /**
  60. * read rl2 header data and setup the avstreams
  61. * @param s demuxer context
  62. * @return 0 on success, AVERROR otherwise
  63. */
  64. static av_cold int rl2_read_header(AVFormatContext *s)
  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 method */
  93. sound_rate = avio_rl16(pb);
  94. rate = avio_rl16(pb);
  95. channels = avio_rl16(pb);
  96. def_sound_size = avio_rl16(pb);
  97. if (!channels || channels > 42) {
  98. av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
  99. return AVERROR_INVALIDDATA;
  100. }
  101. /** setup video stream */
  102. st = avformat_new_stream(s, NULL);
  103. if(!st)
  104. return AVERROR(ENOMEM);
  105. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  106. st->codecpar->codec_id = AV_CODEC_ID_RL2;
  107. st->codecpar->codec_tag = 0; /* no fourcc */
  108. st->codecpar->width = 320;
  109. st->codecpar->height = 200;
  110. /** allocate and fill extradata */
  111. st->codecpar->extradata_size = EXTRADATA1_SIZE;
  112. if(signature == RLV3_TAG && back_size > 0)
  113. st->codecpar->extradata_size += back_size;
  114. st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size +
  115. AV_INPUT_BUFFER_PADDING_SIZE);
  116. if(!st->codecpar->extradata)
  117. return AVERROR(ENOMEM);
  118. if(avio_read(pb,st->codecpar->extradata,st->codecpar->extradata_size) !=
  119. st->codecpar->extradata_size)
  120. return AVERROR(EIO);
  121. /** setup audio stream if present */
  122. if(sound_rate){
  123. pts_num = def_sound_size;
  124. pts_den = rate;
  125. st = avformat_new_stream(s, NULL);
  126. if (!st)
  127. return AVERROR(ENOMEM);
  128. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  129. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  130. st->codecpar->codec_tag = 1;
  131. st->codecpar->channels = channels;
  132. st->codecpar->bits_per_coded_sample = 8;
  133. st->codecpar->sample_rate = rate;
  134. st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
  135. st->codecpar->bits_per_coded_sample;
  136. st->codecpar->block_align = st->codecpar->channels *
  137. st->codecpar->bits_per_coded_sample / 8;
  138. avpriv_set_pts_info(st,32,1,rate);
  139. }
  140. avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
  141. chunk_size = av_malloc(frame_count * sizeof(uint32_t));
  142. audio_size = av_malloc(frame_count * sizeof(uint32_t));
  143. chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
  144. if(!chunk_size || !audio_size || !chunk_offset){
  145. av_free(chunk_size);
  146. av_free(audio_size);
  147. av_free(chunk_offset);
  148. return AVERROR(ENOMEM);
  149. }
  150. /** read offset and size tables */
  151. for(i=0; i < frame_count;i++)
  152. chunk_size[i] = avio_rl32(pb);
  153. for(i=0; i < frame_count;i++)
  154. chunk_offset[i] = avio_rl32(pb);
  155. for(i=0; i < frame_count;i++)
  156. audio_size[i] = avio_rl32(pb) & 0xFFFF;
  157. /** build the sample index */
  158. for(i=0;i<frame_count;i++){
  159. if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
  160. ret = AVERROR_INVALIDDATA;
  161. break;
  162. }
  163. if(sound_rate && audio_size[i]){
  164. av_add_index_entry(s->streams[1], chunk_offset[i],
  165. audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
  166. audio_frame_counter += audio_size[i] / channels;
  167. }
  168. av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
  169. video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
  170. ++video_frame_counter;
  171. }
  172. av_free(chunk_size);
  173. av_free(audio_size);
  174. av_free(chunk_offset);
  175. return ret;
  176. }
  177. /**
  178. * read a single audio or video packet
  179. * @param s demuxer context
  180. * @param pkt the packet to be filled
  181. * @return 0 on success, AVERROR otherwise
  182. */
  183. static int rl2_read_packet(AVFormatContext *s,
  184. AVPacket *pkt)
  185. {
  186. Rl2DemuxContext *rl2 = s->priv_data;
  187. AVIOContext *pb = s->pb;
  188. AVIndexEntry *sample = NULL;
  189. int i;
  190. int ret = 0;
  191. int stream_id = -1;
  192. int64_t pos = INT64_MAX;
  193. /** check if there is a valid video or audio entry that can be used */
  194. for(i=0; i<s->nb_streams; i++){
  195. if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
  196. && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
  197. sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
  198. pos= sample->pos;
  199. stream_id= i;
  200. }
  201. }
  202. if(stream_id == -1)
  203. return AVERROR(EIO);
  204. ++rl2->index_pos[stream_id];
  205. /** position the stream (will probably be there anyway) */
  206. avio_seek(pb, sample->pos, SEEK_SET);
  207. /** fill the packet */
  208. ret = av_get_packet(pb, pkt, sample->size);
  209. if(ret != sample->size){
  210. av_packet_unref(pkt);
  211. return AVERROR(EIO);
  212. }
  213. pkt->stream_index = stream_id;
  214. pkt->pts = sample->timestamp;
  215. return ret;
  216. }
  217. /**
  218. * seek to a new timestamp
  219. * @param s demuxer context
  220. * @param stream_index index of the stream that should be seeked
  221. * @param timestamp wanted timestamp
  222. * @param flags direction and seeking mode
  223. * @return 0 on success, -1 otherwise
  224. */
  225. static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  226. {
  227. AVStream *st = s->streams[stream_index];
  228. Rl2DemuxContext *rl2 = s->priv_data;
  229. int i;
  230. int index = av_index_search_timestamp(st, timestamp, flags);
  231. if(index < 0)
  232. return -1;
  233. rl2->index_pos[stream_index] = index;
  234. timestamp = st->index_entries[index].timestamp;
  235. for(i=0; i < s->nb_streams; i++){
  236. AVStream *st2 = s->streams[i];
  237. index = av_index_search_timestamp(st2,
  238. av_rescale_q(timestamp, st->time_base, st2->time_base),
  239. flags | AVSEEK_FLAG_BACKWARD);
  240. if(index < 0)
  241. index = 0;
  242. rl2->index_pos[i] = index;
  243. }
  244. return 0;
  245. }
  246. AVInputFormat ff_rl2_demuxer = {
  247. .name = "rl2",
  248. .long_name = NULL_IF_CONFIG_SMALL("RL2"),
  249. .priv_data_size = sizeof(Rl2DemuxContext),
  250. .read_probe = rl2_probe,
  251. .read_header = rl2_read_header,
  252. .read_packet = rl2_read_packet,
  253. .read_seek = rl2_read_seek,
  254. };