dsicin.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, AVFormatParameters *ap)
  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 = 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 = 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. if (hdr->video_frame_size < 0 || hdr->audio_frame_size < 0)
  130. return AVERROR_INVALIDDATA;
  131. return 0;
  132. }
  133. static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
  134. {
  135. CinDemuxContext *cin = s->priv_data;
  136. AVIOContext *pb = s->pb;
  137. CinFrameHeader *hdr = &cin->frame_header;
  138. int rc, palette_type, pkt_size;
  139. int ret;
  140. if (cin->audio_buffer_size == 0) {
  141. rc = cin_read_frame_header(cin, pb);
  142. if (rc)
  143. return rc;
  144. if ((int16_t)hdr->pal_colors_count < 0) {
  145. hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
  146. palette_type = 1;
  147. } else {
  148. palette_type = 0;
  149. }
  150. /* palette and video packet */
  151. pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
  152. pkt_size = ffio_limit(pb, pkt_size);
  153. ret = av_new_packet(pkt, 4 + pkt_size);
  154. if (ret < 0)
  155. return ret;
  156. pkt->stream_index = cin->video_stream_index;
  157. pkt->pts = cin->video_stream_pts++;
  158. pkt->data[0] = palette_type;
  159. pkt->data[1] = hdr->pal_colors_count & 0xFF;
  160. pkt->data[2] = hdr->pal_colors_count >> 8;
  161. pkt->data[3] = hdr->video_frame_type;
  162. ret = avio_read(pb, &pkt->data[4], pkt_size);
  163. if (ret < 0) {
  164. av_free_packet(pkt);
  165. return ret;
  166. }
  167. if (ret < pkt_size)
  168. av_shrink_packet(pkt, 4 + ret);
  169. /* sound buffer will be processed on next read_packet() call */
  170. cin->audio_buffer_size = hdr->audio_frame_size;
  171. return 0;
  172. }
  173. /* audio packet */
  174. ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
  175. if (ret < 0)
  176. return ret;
  177. pkt->stream_index = cin->audio_stream_index;
  178. pkt->pts = cin->audio_stream_pts;
  179. pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
  180. cin->audio_stream_pts += pkt->duration;
  181. cin->audio_buffer_size = 0;
  182. return 0;
  183. }
  184. AVInputFormat ff_dsicin_demuxer = {
  185. .name = "dsicin",
  186. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"),
  187. .priv_data_size = sizeof(CinDemuxContext),
  188. .read_probe = cin_probe,
  189. .read_header = cin_read_header,
  190. .read_packet = cin_read_packet,
  191. };