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.

175 lines
5.8KB

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