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.

171 lines
5.6KB

  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;
  57. uint16_t audio_size, palette_size;
  58. int32_t video_size;
  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. palette_size = AV_RB16(&cdxl->header[20]);
  73. audio_size = AV_RB16(&cdxl->header[22]);
  74. if (palette_size > 512)
  75. return AVERROR_INVALIDDATA;
  76. if (current_size < audio_size + palette_size + CDXL_HEADER_SIZE)
  77. return AVERROR_INVALIDDATA;
  78. video_size = current_size - audio_size - CDXL_HEADER_SIZE;
  79. if (cdxl->read_chunk && audio_size) {
  80. if (cdxl->audio_stream_index == -1) {
  81. AVStream *st = avformat_new_stream(s, NULL);
  82. if (!st)
  83. return AVERROR(ENOMEM);
  84. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  85. st->codec->codec_tag = 0;
  86. st->codec->codec_id = CODEC_ID_PCM_S8;
  87. st->codec->channels = cdxl->header[1] & 0x10 ? 2 : 1;
  88. st->codec->sample_rate = cdxl->sample_rate;
  89. cdxl->audio_stream_index = st->index;
  90. avpriv_set_pts_info(st, 32, 1, cdxl->sample_rate);
  91. }
  92. ret = av_get_packet(pb, pkt, audio_size);
  93. if (ret < 0)
  94. return ret;
  95. pkt->stream_index = cdxl->audio_stream_index;
  96. pkt->pos = pos;
  97. pkt->duration = audio_size;
  98. cdxl->read_chunk = 0;
  99. } else {
  100. if (cdxl->video_stream_index == -1) {
  101. AVStream *st = avformat_new_stream(s, NULL);
  102. if (!st)
  103. return AVERROR(ENOMEM);
  104. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  105. st->codec->codec_tag = 0;
  106. st->codec->codec_id = CODEC_ID_CDXL;
  107. st->codec->width = AV_RB16(&cdxl->header[14]);
  108. st->codec->height = AV_RB16(&cdxl->header[16]);
  109. cdxl->video_stream_index = st->index;
  110. avpriv_set_pts_info(st, 63, cdxl->fps.den, cdxl->fps.num);
  111. }
  112. if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
  113. return AVERROR(ENOMEM);
  114. memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
  115. ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
  116. if (ret < 0) {
  117. av_free_packet(pkt);
  118. return ret;
  119. }
  120. pkt->stream_index = cdxl->video_stream_index;
  121. pkt->flags |= AV_PKT_FLAG_KEY;
  122. pkt->pos = pos;
  123. cdxl->read_chunk = audio_size;
  124. }
  125. return ret;
  126. }
  127. #define OFFSET(x) offsetof(CDXLDemuxContext, x)
  128. static const AVOption cdxl_options[] = {
  129. { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .dbl = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  130. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "10" }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  131. { NULL },
  132. };
  133. static const AVClass cdxl_demuxer_class = {
  134. .class_name = "CDXL demuxer",
  135. .item_name = av_default_item_name,
  136. .option = cdxl_options,
  137. .version = LIBAVUTIL_VERSION_INT,
  138. };
  139. AVInputFormat ff_cdxl_demuxer = {
  140. .name = "cdxl",
  141. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video format"),
  142. .priv_data_size = sizeof(CDXLDemuxContext),
  143. .read_header = cdxl_read_header,
  144. .read_packet = cdxl_read_packet,
  145. .extensions = "cdxl,xl",
  146. .flags = AVFMT_GENERIC_INDEX,
  147. .priv_class = &cdxl_demuxer_class,
  148. };