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.

296 lines
8.6KB

  1. /*
  2. * Id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file idroq.c
  21. * Id RoQ format file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the .roq file format, visit:
  24. * http://www.csse.monash.edu.au/~timf/
  25. */
  26. #include "avformat.h"
  27. #define RoQ_MAGIC_NUMBER 0x1084
  28. #define RoQ_CHUNK_PREAMBLE_SIZE 8
  29. #define RoQ_AUDIO_SAMPLE_RATE 22050
  30. #define RoQ_CHUNKS_TO_SCAN 30
  31. #define RoQ_INFO 0x1001
  32. #define RoQ_QUAD_CODEBOOK 0x1002
  33. #define RoQ_QUAD_VQ 0x1011
  34. #define RoQ_SOUND_MONO 0x1020
  35. #define RoQ_SOUND_STEREO 0x1021
  36. typedef struct RoqDemuxContext {
  37. int width;
  38. int height;
  39. int audio_channels;
  40. int framerate;
  41. int frame_pts_inc;
  42. int video_stream_index;
  43. int audio_stream_index;
  44. int64_t video_pts;
  45. unsigned int audio_frame_count;
  46. } RoqDemuxContext;
  47. static int roq_probe(AVProbeData *p)
  48. {
  49. if (p->buf_size < 6)
  50. return 0;
  51. if ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  52. (LE_32(&p->buf[2]) != 0xFFFFFFFF))
  53. return 0;
  54. return AVPROBE_SCORE_MAX;
  55. }
  56. static int roq_read_header(AVFormatContext *s,
  57. AVFormatParameters *ap)
  58. {
  59. RoqDemuxContext *roq = s->priv_data;
  60. ByteIOContext *pb = &s->pb;
  61. AVStream *st;
  62. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  63. int i;
  64. unsigned int chunk_size;
  65. unsigned int chunk_type;
  66. /* get the main header */
  67. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  68. RoQ_CHUNK_PREAMBLE_SIZE)
  69. return AVERROR_IO;
  70. roq->framerate = LE_16(&preamble[6]);
  71. roq->frame_pts_inc = 90000 / roq->framerate;
  72. /* set the pts reference (1 pts = 1/90000) */
  73. s->pts_num = 1;
  74. s->pts_den = 90000;
  75. /* init private context parameters */
  76. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  77. roq->audio_frame_count = 0;
  78. /* scan the first n chunks searching for A/V parameters */
  79. for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
  80. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  81. RoQ_CHUNK_PREAMBLE_SIZE)
  82. return AVERROR_IO;
  83. chunk_type = LE_16(&preamble[0]);
  84. chunk_size = LE_32(&preamble[2]);
  85. switch (chunk_type) {
  86. case RoQ_INFO:
  87. /* fetch the width and height; reuse the preamble bytes */
  88. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  89. RoQ_CHUNK_PREAMBLE_SIZE)
  90. return AVERROR_IO;
  91. roq->width = LE_16(&preamble[0]);
  92. roq->height = LE_16(&preamble[2]);
  93. break;
  94. case RoQ_QUAD_CODEBOOK:
  95. case RoQ_QUAD_VQ:
  96. /* ignore during this scan */
  97. url_fseek(pb, chunk_size, SEEK_CUR);
  98. break;
  99. case RoQ_SOUND_MONO:
  100. roq->audio_channels = 1;
  101. url_fseek(pb, chunk_size, SEEK_CUR);
  102. break;
  103. case RoQ_SOUND_STEREO:
  104. roq->audio_channels = 2;
  105. url_fseek(pb, chunk_size, SEEK_CUR);
  106. break;
  107. default:
  108. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", LE_16(&preamble[0]));
  109. return AVERROR_INVALIDDATA;
  110. break;
  111. }
  112. /* if all necessary parameters have been gathered, exit early */
  113. if ((roq->width && roq->height) && roq->audio_channels)
  114. break;
  115. }
  116. /* seek back to the first chunk */
  117. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
  118. /* initialize the decoders */
  119. st = av_new_stream(s, 0);
  120. if (!st)
  121. return AVERROR_NOMEM;
  122. roq->video_stream_index = st->index;
  123. st->codec.codec_type = CODEC_TYPE_VIDEO;
  124. st->codec.codec_id = CODEC_ID_ROQ;
  125. st->codec.codec_tag = 0; /* no fourcc */
  126. st->codec.width = roq->width;
  127. st->codec.height = roq->height;
  128. if (roq->audio_channels) {
  129. st = av_new_stream(s, 0);
  130. if (!st)
  131. return AVERROR_NOMEM;
  132. roq->audio_stream_index = st->index;
  133. st->codec.codec_type = CODEC_TYPE_AUDIO;
  134. st->codec.codec_id = CODEC_ID_ROQ_DPCM;
  135. st->codec.codec_tag = 0; /* no tag */
  136. st->codec.channels = roq->audio_channels;
  137. st->codec.sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  138. st->codec.bits_per_sample = 16;
  139. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  140. st->codec.bits_per_sample;
  141. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  142. }
  143. return 0;
  144. }
  145. static int roq_read_packet(AVFormatContext *s,
  146. AVPacket *pkt)
  147. {
  148. RoqDemuxContext *roq = s->priv_data;
  149. ByteIOContext *pb = &s->pb;
  150. int ret = 0;
  151. unsigned int chunk_size;
  152. unsigned int chunk_type;
  153. unsigned int codebook_size;
  154. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  155. int packet_read = 0;
  156. offset_t codebook_offset;
  157. while (!packet_read) {
  158. if (url_feof(&s->pb))
  159. return -EIO;
  160. /* get the next chunk preamble */
  161. if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  162. RoQ_CHUNK_PREAMBLE_SIZE)
  163. return -EIO;
  164. chunk_type = LE_16(&preamble[0]);
  165. chunk_size = LE_32(&preamble[2]);
  166. switch (chunk_type) {
  167. case RoQ_INFO:
  168. /* don't care about this chunk anymore */
  169. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  170. break;
  171. case RoQ_QUAD_CODEBOOK:
  172. /* packet needs to contain both this codebook and next VQ chunk */
  173. codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  174. codebook_size = chunk_size;
  175. url_fseek(pb, codebook_size, SEEK_CUR);
  176. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  177. RoQ_CHUNK_PREAMBLE_SIZE)
  178. return -EIO;
  179. chunk_size = LE_32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  180. codebook_size;
  181. /* rewind */
  182. url_fseek(pb, codebook_offset, SEEK_SET);
  183. /* load up the packet */
  184. if (av_new_packet(pkt, chunk_size))
  185. return -EIO;
  186. pkt->stream_index = roq->video_stream_index;
  187. pkt->pts = roq->video_pts;
  188. ret = get_buffer(pb, pkt->data, chunk_size);
  189. if (ret != chunk_size)
  190. ret = -EIO;
  191. roq->video_pts += roq->frame_pts_inc;
  192. packet_read = 1;
  193. break;
  194. case RoQ_SOUND_MONO:
  195. case RoQ_SOUND_STEREO:
  196. case RoQ_QUAD_VQ:
  197. /* load up the packet */
  198. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  199. return -EIO;
  200. /* copy over preamble */
  201. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  202. if (chunk_type == RoQ_QUAD_VQ) {
  203. pkt->stream_index = roq->video_stream_index;
  204. pkt->pts = roq->video_pts;
  205. roq->video_pts += roq->frame_pts_inc;
  206. } else {
  207. pkt->stream_index = roq->audio_stream_index;
  208. pkt->pts = roq->audio_frame_count;
  209. pkt->pts *= 90000;
  210. pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
  211. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  212. }
  213. ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  214. chunk_size);
  215. if (ret != chunk_size)
  216. ret = -EIO;
  217. packet_read = 1;
  218. break;
  219. default:
  220. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  221. return AVERROR_INVALIDDATA;
  222. break;
  223. }
  224. }
  225. return ret;
  226. }
  227. static int roq_read_close(AVFormatContext *s)
  228. {
  229. // RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
  230. return 0;
  231. }
  232. static AVInputFormat roq_iformat = {
  233. "RoQ",
  234. "Id RoQ format",
  235. sizeof(RoqDemuxContext),
  236. roq_probe,
  237. roq_read_header,
  238. roq_read_packet,
  239. roq_read_close,
  240. };
  241. int roq_init(void)
  242. {
  243. av_register_input_format(&roq_iformat);
  244. return 0;
  245. }