dsicin.c 6.6 KB

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