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.

245 lines
8.0KB

  1. /*
  2. * id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  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. * @file
  23. * id RoQ format file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .roq file format, visit:
  26. * http://www.csse.monash.edu.au/~timf/
  27. */
  28. #include "libavutil/channel_layout.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "avio_internal.h"
  33. #define RoQ_MAGIC_NUMBER 0x1084
  34. #define RoQ_CHUNK_PREAMBLE_SIZE 8
  35. #define RoQ_AUDIO_SAMPLE_RATE 22050
  36. #define RoQ_CHUNKS_TO_SCAN 30
  37. #define RoQ_INFO 0x1001
  38. #define RoQ_QUAD_CODEBOOK 0x1002
  39. #define RoQ_QUAD_VQ 0x1011
  40. #define RoQ_SOUND_MONO 0x1020
  41. #define RoQ_SOUND_STEREO 0x1021
  42. typedef struct RoqDemuxContext {
  43. int frame_rate;
  44. int width;
  45. int height;
  46. int audio_channels;
  47. int video_stream_index;
  48. int audio_stream_index;
  49. int64_t video_pts;
  50. unsigned int audio_frame_count;
  51. } RoqDemuxContext;
  52. static int roq_probe(AVProbeData *p)
  53. {
  54. if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  55. (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
  56. return 0;
  57. return AVPROBE_SCORE_MAX;
  58. }
  59. static int roq_read_header(AVFormatContext *s)
  60. {
  61. RoqDemuxContext *roq = s->priv_data;
  62. AVIOContext *pb = s->pb;
  63. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  64. /* get the main header */
  65. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  66. RoQ_CHUNK_PREAMBLE_SIZE)
  67. return AVERROR(EIO);
  68. roq->frame_rate = AV_RL16(&preamble[6]);
  69. /* init private context parameters */
  70. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  71. roq->audio_frame_count = 0;
  72. roq->audio_stream_index = -1;
  73. roq->video_stream_index = -1;
  74. s->ctx_flags |= AVFMTCTX_NOHEADER;
  75. return 0;
  76. }
  77. static int roq_read_packet(AVFormatContext *s,
  78. AVPacket *pkt)
  79. {
  80. RoqDemuxContext *roq = s->priv_data;
  81. AVIOContext *pb = s->pb;
  82. int ret = 0;
  83. unsigned int chunk_size;
  84. unsigned int chunk_type;
  85. unsigned int codebook_size;
  86. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  87. int packet_read = 0;
  88. int64_t codebook_offset;
  89. while (!packet_read) {
  90. if (url_feof(s->pb))
  91. return AVERROR(EIO);
  92. /* get the next chunk preamble */
  93. if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  94. RoQ_CHUNK_PREAMBLE_SIZE)
  95. return AVERROR(EIO);
  96. chunk_type = AV_RL16(&preamble[0]);
  97. chunk_size = AV_RL32(&preamble[2]);
  98. if(chunk_size > INT_MAX)
  99. return AVERROR_INVALIDDATA;
  100. chunk_size = ffio_limit(pb, chunk_size);
  101. switch (chunk_type) {
  102. case RoQ_INFO:
  103. if (roq->video_stream_index == -1) {
  104. AVStream *st = avformat_new_stream(s, NULL);
  105. if (!st)
  106. return AVERROR(ENOMEM);
  107. avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
  108. roq->video_stream_index = st->index;
  109. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  110. st->codec->codec_id = AV_CODEC_ID_ROQ;
  111. st->codec->codec_tag = 0; /* no fourcc */
  112. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
  113. return AVERROR(EIO);
  114. st->codec->width = roq->width = AV_RL16(preamble);
  115. st->codec->height = roq->height = AV_RL16(preamble + 2);
  116. break;
  117. }
  118. /* don't care about this chunk anymore */
  119. avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
  120. break;
  121. case RoQ_QUAD_CODEBOOK:
  122. if (roq->video_stream_index < 0)
  123. return AVERROR_INVALIDDATA;
  124. /* packet needs to contain both this codebook and next VQ chunk */
  125. codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  126. codebook_size = chunk_size;
  127. avio_skip(pb, codebook_size);
  128. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  129. RoQ_CHUNK_PREAMBLE_SIZE)
  130. return AVERROR(EIO);
  131. chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  132. codebook_size;
  133. /* rewind */
  134. avio_seek(pb, codebook_offset, SEEK_SET);
  135. /* load up the packet */
  136. ret= av_get_packet(pb, pkt, chunk_size);
  137. if (ret != chunk_size)
  138. return AVERROR(EIO);
  139. pkt->stream_index = roq->video_stream_index;
  140. pkt->pts = roq->video_pts++;
  141. packet_read = 1;
  142. break;
  143. case RoQ_SOUND_MONO:
  144. case RoQ_SOUND_STEREO:
  145. if (roq->audio_stream_index == -1) {
  146. AVStream *st = avformat_new_stream(s, NULL);
  147. if (!st)
  148. return AVERROR(ENOMEM);
  149. avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
  150. roq->audio_stream_index = st->index;
  151. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  152. st->codec->codec_id = AV_CODEC_ID_ROQ_DPCM;
  153. st->codec->codec_tag = 0; /* no tag */
  154. if (chunk_type == RoQ_SOUND_STEREO) {
  155. st->codec->channels = 2;
  156. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  157. } else {
  158. st->codec->channels = 1;
  159. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  160. }
  161. roq->audio_channels = st->codec->channels;
  162. st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  163. st->codec->bits_per_coded_sample = 16;
  164. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  165. st->codec->bits_per_coded_sample;
  166. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  167. }
  168. case RoQ_QUAD_VQ:
  169. if (chunk_type == RoQ_QUAD_VQ) {
  170. if (roq->video_stream_index < 0)
  171. return AVERROR_INVALIDDATA;
  172. }
  173. /* load up the packet */
  174. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  175. return AVERROR(EIO);
  176. /* copy over preamble */
  177. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  178. if (chunk_type == RoQ_QUAD_VQ) {
  179. pkt->stream_index = roq->video_stream_index;
  180. pkt->pts = roq->video_pts++;
  181. } else {
  182. pkt->stream_index = roq->audio_stream_index;
  183. pkt->pts = roq->audio_frame_count;
  184. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  185. }
  186. pkt->pos= avio_tell(pb);
  187. ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  188. chunk_size);
  189. if (ret != chunk_size)
  190. ret = AVERROR(EIO);
  191. packet_read = 1;
  192. break;
  193. default:
  194. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  195. return AVERROR_INVALIDDATA;
  196. }
  197. }
  198. return ret;
  199. }
  200. AVInputFormat ff_roq_demuxer = {
  201. .name = "roq",
  202. .long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
  203. .priv_data_size = sizeof(RoqDemuxContext),
  204. .read_probe = roq_probe,
  205. .read_header = roq_read_header,
  206. .read_packet = roq_read_packet,
  207. };