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.

228 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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. typedef struct CinFileHeader {
  29. int video_frame_size;
  30. int video_frame_width;
  31. int video_frame_height;
  32. int audio_frequency;
  33. int audio_bits;
  34. int audio_stereo;
  35. int audio_frame_size;
  36. } CinFileHeader;
  37. typedef struct CinFrameHeader {
  38. int audio_frame_type;
  39. int video_frame_type;
  40. int pal_colors_count;
  41. int audio_frame_size;
  42. int video_frame_size;
  43. } CinFrameHeader;
  44. typedef struct CinDemuxContext {
  45. int audio_stream_index;
  46. int video_stream_index;
  47. CinFileHeader file_header;
  48. int64_t audio_stream_pts;
  49. int64_t video_stream_pts;
  50. CinFrameHeader frame_header;
  51. int audio_buffer_size;
  52. } CinDemuxContext;
  53. static int cin_probe(AVProbeData *p)
  54. {
  55. /* header starts with this special marker */
  56. if (AV_RL32(&p->buf[0]) != 0x55AA0000)
  57. return 0;
  58. /* for accuracy, check some header field values */
  59. if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
  60. return 0;
  61. return AVPROBE_SCORE_MAX;
  62. }
  63. static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb) {
  64. CinFileHeader *hdr = &cin->file_header;
  65. if (avio_rl32(pb) != 0x55AA0000)
  66. return AVERROR_INVALIDDATA;
  67. hdr->video_frame_size = avio_rl32(pb);
  68. hdr->video_frame_width = avio_rl16(pb);
  69. hdr->video_frame_height = avio_rl16(pb);
  70. hdr->audio_frequency = avio_rl32(pb);
  71. hdr->audio_bits = avio_r8(pb);
  72. hdr->audio_stereo = avio_r8(pb);
  73. hdr->audio_frame_size = avio_rl16(pb);
  74. if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
  75. return AVERROR_INVALIDDATA;
  76. return 0;
  77. }
  78. static int cin_read_header(AVFormatContext *s)
  79. {
  80. int rc;
  81. CinDemuxContext *cin = s->priv_data;
  82. CinFileHeader *hdr = &cin->file_header;
  83. AVIOContext *pb = s->pb;
  84. AVStream *st;
  85. rc = cin_read_file_header(cin, pb);
  86. if (rc)
  87. return rc;
  88. cin->video_stream_pts = 0;
  89. cin->audio_stream_pts = 0;
  90. cin->audio_buffer_size = 0;
  91. /* initialize the video decoder stream */
  92. st = avformat_new_stream(s, NULL);
  93. if (!st)
  94. return AVERROR(ENOMEM);
  95. avpriv_set_pts_info(st, 32, 1, 12);
  96. cin->video_stream_index = st->index;
  97. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  98. st->codec->codec_id = CODEC_ID_DSICINVIDEO;
  99. st->codec->codec_tag = 0; /* no fourcc */
  100. st->codec->width = hdr->video_frame_width;
  101. st->codec->height = hdr->video_frame_height;
  102. /* initialize the audio decoder stream */
  103. st = avformat_new_stream(s, NULL);
  104. if (!st)
  105. return AVERROR(ENOMEM);
  106. avpriv_set_pts_info(st, 32, 1, 22050);
  107. cin->audio_stream_index = st->index;
  108. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  109. st->codec->codec_id = CODEC_ID_DSICINAUDIO;
  110. st->codec->codec_tag = 0; /* no tag */
  111. st->codec->channels = 1;
  112. st->codec->sample_rate = 22050;
  113. st->codec->bits_per_coded_sample = 8;
  114. st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample * st->codec->channels;
  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 (pb->eof_reached || 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. pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
  176. cin->audio_stream_pts += pkt->duration;
  177. cin->audio_buffer_size = 0;
  178. return 0;
  179. }
  180. AVInputFormat ff_dsicin_demuxer = {
  181. .name = "dsicin",
  182. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"),
  183. .priv_data_size = sizeof(CinDemuxContext),
  184. .read_probe = cin_probe,
  185. .read_header = cin_read_header,
  186. .read_packet = cin_read_packet,
  187. };