dsicin.c 6.5 KB

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