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.

189 lines
6.4KB

  1. /*
  2. * CDXL demuxer
  3. * Copyright (c) 2011-2012 Paul B Mahol
  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. #include "libavutil/channel_layout.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/opt.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #define CDXL_HEADER_SIZE 32
  28. typedef struct CDXLDemuxContext {
  29. AVClass *class;
  30. int sample_rate;
  31. char *framerate;
  32. AVRational fps;
  33. int read_chunk;
  34. uint8_t header[CDXL_HEADER_SIZE];
  35. int video_stream_index;
  36. int audio_stream_index;
  37. } CDXLDemuxContext;
  38. static int cdxl_read_header(AVFormatContext *s)
  39. {
  40. CDXLDemuxContext *cdxl = s->priv_data;
  41. int ret;
  42. if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
  43. av_log(s, AV_LOG_ERROR,
  44. "Could not parse framerate: %s.\n", cdxl->framerate);
  45. return ret;
  46. }
  47. cdxl->read_chunk = 0;
  48. cdxl->video_stream_index = -1;
  49. cdxl->audio_stream_index = -1;
  50. s->ctx_flags |= AVFMTCTX_NOHEADER;
  51. return 0;
  52. }
  53. static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
  54. {
  55. CDXLDemuxContext *cdxl = s->priv_data;
  56. AVIOContext *pb = s->pb;
  57. uint32_t current_size, video_size, image_size;
  58. uint16_t audio_size, palette_size, width, height;
  59. int64_t pos;
  60. int ret;
  61. if (pb->eof_reached)
  62. return AVERROR_EOF;
  63. pos = avio_tell(pb);
  64. if (!cdxl->read_chunk &&
  65. avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
  66. return AVERROR_EOF;
  67. if (cdxl->header[0] != 1) {
  68. av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
  69. return AVERROR_INVALIDDATA;
  70. }
  71. current_size = AV_RB32(&cdxl->header[2]);
  72. width = AV_RB16(&cdxl->header[14]);
  73. height = AV_RB16(&cdxl->header[16]);
  74. palette_size = AV_RB16(&cdxl->header[20]);
  75. audio_size = AV_RB16(&cdxl->header[22]);
  76. image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
  77. video_size = palette_size + image_size;
  78. if (palette_size > 512)
  79. return AVERROR_INVALIDDATA;
  80. if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
  81. return AVERROR_INVALIDDATA;
  82. if (cdxl->read_chunk && audio_size) {
  83. if (cdxl->audio_stream_index == -1) {
  84. AVStream *st = avformat_new_stream(s, NULL);
  85. if (!st)
  86. return AVERROR(ENOMEM);
  87. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  88. st->codecpar->codec_tag = 0;
  89. st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
  90. if (cdxl->header[1] & 0x10) {
  91. st->codecpar->channels = 2;
  92. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  93. } else {
  94. st->codecpar->channels = 1;
  95. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  96. }
  97. st->codecpar->sample_rate = cdxl->sample_rate;
  98. st->start_time = 0;
  99. cdxl->audio_stream_index = st->index;
  100. avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
  101. }
  102. ret = av_get_packet(pb, pkt, audio_size);
  103. if (ret < 0)
  104. return ret;
  105. pkt->stream_index = cdxl->audio_stream_index;
  106. pkt->pos = pos;
  107. pkt->duration = audio_size;
  108. cdxl->read_chunk = 0;
  109. } else {
  110. if (cdxl->video_stream_index == -1) {
  111. AVStream *st = avformat_new_stream(s, NULL);
  112. if (!st)
  113. return AVERROR(ENOMEM);
  114. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  115. st->codecpar->codec_tag = 0;
  116. st->codecpar->codec_id = AV_CODEC_ID_CDXL;
  117. st->codecpar->width = width;
  118. st->codecpar->height = height;
  119. st->start_time = 0;
  120. cdxl->video_stream_index = st->index;
  121. if (cdxl->framerate)
  122. avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
  123. else
  124. avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
  125. }
  126. if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
  127. return AVERROR(ENOMEM);
  128. memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
  129. ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
  130. if (ret < 0) {
  131. av_packet_unref(pkt);
  132. return ret;
  133. }
  134. av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
  135. pkt->stream_index = cdxl->video_stream_index;
  136. pkt->flags |= AV_PKT_FLAG_KEY;
  137. pkt->pos = pos;
  138. pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
  139. cdxl->read_chunk = audio_size;
  140. }
  141. if (!cdxl->read_chunk)
  142. avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
  143. return ret;
  144. }
  145. #define OFFSET(x) offsetof(CDXLDemuxContext, x)
  146. static const AVOption cdxl_options[] = {
  147. { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  148. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  149. { NULL },
  150. };
  151. static const AVClass cdxl_demuxer_class = {
  152. .class_name = "CDXL demuxer",
  153. .item_name = av_default_item_name,
  154. .option = cdxl_options,
  155. .version = LIBAVUTIL_VERSION_INT,
  156. };
  157. AVInputFormat ff_cdxl_demuxer = {
  158. .name = "cdxl",
  159. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
  160. .priv_data_size = sizeof(CDXLDemuxContext),
  161. .read_header = cdxl_read_header,
  162. .read_packet = cdxl_read_packet,
  163. .extensions = "cdxl,xl",
  164. .flags = AVFMT_GENERIC_INDEX,
  165. .priv_class = &cdxl_demuxer_class,
  166. };