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.

225 lines
6.4KB

  1. /*
  2. * Delphine Software International CIN File Demuxer
  3. * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
  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 dsicin.c
  23. * Delphine Software International CIN file demuxer
  24. */
  25. #include "avformat.h"
  26. typedef struct CinFileHeader {
  27. int video_frame_size;
  28. int video_frame_width;
  29. int video_frame_height;
  30. int audio_frequency;
  31. int audio_bits;
  32. int audio_stereo;
  33. int audio_frame_size;
  34. } CinFileHeader;
  35. typedef struct CinFrameHeader {
  36. int audio_frame_type;
  37. int video_frame_type;
  38. int pal_colors_count;
  39. int audio_frame_size;
  40. int video_frame_size;
  41. } CinFrameHeader;
  42. typedef struct CinDemuxContext {
  43. int audio_stream_index;
  44. int video_stream_index;
  45. CinFileHeader file_header;
  46. int64_t audio_stream_pts;
  47. int64_t video_stream_pts;
  48. CinFrameHeader frame_header;
  49. int audio_buffer_size;
  50. } CinDemuxContext;
  51. static int cin_probe(AVProbeData *p)
  52. {
  53. if (p->buf_size < 18)
  54. return 0;
  55. /* header starts with this special marker */
  56. if (AV_RL32(&p->buf[0]) != 0x55AA0000)
  57. return 0;
  58. /* for accuracy, check some header field values */
  59. if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
  60. return 0;
  61. return AVPROBE_SCORE_MAX;
  62. }
  63. static int cin_read_file_header(CinDemuxContext *cin, ByteIOContext *pb) {
  64. CinFileHeader *hdr = &cin->file_header;
  65. if (get_le32(pb) != 0x55AA0000)
  66. return AVERROR_INVALIDDATA;
  67. hdr->video_frame_size = get_le32(pb);
  68. hdr->video_frame_width = get_le16(pb);
  69. hdr->video_frame_height = get_le16(pb);
  70. hdr->audio_frequency = get_le32(pb);
  71. hdr->audio_bits = get_byte(pb);
  72. hdr->audio_stereo = get_byte(pb);
  73. hdr->audio_frame_size = get_le16(pb);
  74. if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
  75. return AVERROR_INVALIDDATA;
  76. return 0;
  77. }
  78. static int cin_read_header(AVFormatContext *s, AVFormatParameters *ap)
  79. {
  80. int rc;
  81. CinDemuxContext *cin = (CinDemuxContext *)s->priv_data;
  82. CinFileHeader *hdr = &cin->file_header;
  83. ByteIOContext *pb = &s->pb;
  84. AVStream *st;
  85. rc = cin_read_file_header(cin, pb);
  86. if (rc)
  87. return rc;
  88. cin->video_stream_pts = 0;
  89. cin->audio_stream_pts = 0;
  90. cin->audio_buffer_size = 0;
  91. /* initialize the video decoder stream */
  92. st = av_new_stream(s, 0);
  93. if (!st)
  94. return AVERROR_NOMEM;
  95. av_set_pts_info(st, 32, 1, 12);
  96. cin->video_stream_index = st->index;
  97. st->codec->codec_type = CODEC_TYPE_VIDEO;
  98. st->codec->codec_id = CODEC_ID_DSICINVIDEO;
  99. st->codec->codec_tag = 0; /* no fourcc */
  100. st->codec->width = hdr->video_frame_width;
  101. st->codec->height = hdr->video_frame_height;
  102. /* initialize the audio decoder stream */
  103. st = av_new_stream(s, 0);
  104. if (!st)
  105. return AVERROR_NOMEM;
  106. av_set_pts_info(st, 32, 1, 22050);
  107. cin->audio_stream_index = st->index;
  108. st->codec->codec_type = CODEC_TYPE_AUDIO;
  109. st->codec->codec_id = CODEC_ID_DSICINAUDIO;
  110. st->codec->codec_tag = 0; /* no tag */
  111. st->codec->channels = 1;
  112. st->codec->sample_rate = 22050;
  113. st->codec->bits_per_sample = 16;
  114. st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample * st->codec->channels;
  115. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  116. return 0;
  117. }
  118. static int cin_read_frame_header(CinDemuxContext *cin, ByteIOContext *pb) {
  119. CinFrameHeader *hdr = &cin->frame_header;
  120. hdr->video_frame_type = get_byte(pb);
  121. hdr->audio_frame_type = get_byte(pb);
  122. hdr->pal_colors_count = get_le16(pb);
  123. hdr->video_frame_size = get_le32(pb);
  124. hdr->audio_frame_size = get_le32(pb);
  125. if (url_feof(pb) || url_ferror(pb))
  126. return AVERROR_IO;
  127. if (get_le32(pb) != 0xAA55AA55)
  128. return AVERROR_INVALIDDATA;
  129. return 0;
  130. }
  131. static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
  132. {
  133. CinDemuxContext *cin = (CinDemuxContext *)s->priv_data;
  134. ByteIOContext *pb = &s->pb;
  135. CinFrameHeader *hdr = &cin->frame_header;
  136. int rc, palette_type, pkt_size;
  137. if (cin->audio_buffer_size == 0) {
  138. rc = cin_read_frame_header(cin, pb);
  139. if (rc)
  140. return rc;
  141. if ((int16_t)hdr->pal_colors_count < 0) {
  142. hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
  143. palette_type = 1;
  144. } else {
  145. palette_type = 0;
  146. }
  147. /* palette and video packet */
  148. pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
  149. if (av_new_packet(pkt, 4 + pkt_size))
  150. return AVERROR_NOMEM;
  151. pkt->stream_index = cin->video_stream_index;
  152. pkt->pts = cin->video_stream_pts++;
  153. pkt->data[0] = palette_type;
  154. pkt->data[1] = hdr->pal_colors_count & 0xFF;
  155. pkt->data[2] = hdr->pal_colors_count >> 8;
  156. pkt->data[3] = hdr->video_frame_type;
  157. if (get_buffer(pb, &pkt->data[4], pkt_size) != pkt_size)
  158. return AVERROR_IO;
  159. /* sound buffer will be processed on next read_packet() call */
  160. cin->audio_buffer_size = hdr->audio_frame_size;
  161. return 0;
  162. }
  163. /* audio packet */
  164. if (av_new_packet(pkt, cin->audio_buffer_size))
  165. return AVERROR_NOMEM;
  166. pkt->stream_index = cin->audio_stream_index;
  167. pkt->pts = cin->audio_stream_pts;
  168. cin->audio_stream_pts += cin->audio_buffer_size * 2 / cin->file_header.audio_frame_size;
  169. if (get_buffer(pb, pkt->data, cin->audio_buffer_size) != cin->audio_buffer_size)
  170. return AVERROR_IO;
  171. cin->audio_buffer_size = 0;
  172. return 0;
  173. }
  174. AVInputFormat dsicin_demuxer = {
  175. "dsicin",
  176. "Delphine Software International CIN format",
  177. sizeof(CinDemuxContext),
  178. cin_probe,
  179. cin_read_header,
  180. cin_read_packet,
  181. };