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.

235 lines
6.8KB

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