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.

227 lines
6.5KB

  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 = avformat_new_stream(s, NULL);
  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 = avformat_new_stream(s, NULL);
  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 = 8;
  113. st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample * st->codec->channels;
  114. return 0;
  115. }
  116. static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb) {
  117. CinFrameHeader *hdr = &cin->frame_header;
  118. hdr->video_frame_type = avio_r8(pb);
  119. hdr->audio_frame_type = avio_r8(pb);
  120. hdr->pal_colors_count = avio_rl16(pb);
  121. hdr->video_frame_size = avio_rl32(pb);
  122. hdr->audio_frame_size = avio_rl32(pb);
  123. if (url_feof(pb) || pb->error)
  124. return AVERROR(EIO);
  125. if (avio_rl32(pb) != 0xAA55AA55)
  126. return AVERROR_INVALIDDATA;
  127. return 0;
  128. }
  129. static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
  130. {
  131. CinDemuxContext *cin = s->priv_data;
  132. AVIOContext *pb = s->pb;
  133. CinFrameHeader *hdr = &cin->frame_header;
  134. int rc, palette_type, pkt_size;
  135. int ret;
  136. if (cin->audio_buffer_size == 0) {
  137. rc = cin_read_frame_header(cin, pb);
  138. if (rc)
  139. return rc;
  140. if ((int16_t)hdr->pal_colors_count < 0) {
  141. hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
  142. palette_type = 1;
  143. } else {
  144. palette_type = 0;
  145. }
  146. /* palette and video packet */
  147. pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
  148. ret = av_new_packet(pkt, 4 + pkt_size);
  149. if (ret < 0)
  150. return ret;
  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. ret = avio_read(pb, &pkt->data[4], pkt_size);
  158. if (ret < 0) {
  159. av_free_packet(pkt);
  160. return ret;
  161. }
  162. if (ret < pkt_size)
  163. av_shrink_packet(pkt, 4 + ret);
  164. /* sound buffer will be processed on next read_packet() call */
  165. cin->audio_buffer_size = hdr->audio_frame_size;
  166. return 0;
  167. }
  168. /* audio packet */
  169. ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
  170. if (ret < 0)
  171. return ret;
  172. pkt->stream_index = cin->audio_stream_index;
  173. pkt->pts = cin->audio_stream_pts;
  174. pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
  175. cin->audio_stream_pts += pkt->duration;
  176. cin->audio_buffer_size = 0;
  177. return 0;
  178. }
  179. AVInputFormat ff_dsicin_demuxer = {
  180. .name = "dsicin",
  181. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"),
  182. .priv_data_size = sizeof(CinDemuxContext),
  183. .read_probe = cin_probe,
  184. .read_header = cin_read_header,
  185. .read_packet = cin_read_packet,
  186. };