cdxl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * CDXL demuxer
  3. * Copyright (c) 2011-2012 Paul B Mahol
  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. #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 (cdxl->framerate && (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 = AV_CODEC_ID_PCM_S8;
  89. st->codec->channels = cdxl->header[1] & 0x10 ? 2 : 1;
  90. st->codec->sample_rate = cdxl->sample_rate;
  91. st->start_time = 0;
  92. cdxl->audio_stream_index = st->index;
  93. avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
  94. }
  95. ret = av_get_packet(pb, pkt, audio_size);
  96. if (ret < 0)
  97. return ret;
  98. pkt->stream_index = cdxl->audio_stream_index;
  99. pkt->pos = pos;
  100. pkt->duration = audio_size;
  101. cdxl->read_chunk = 0;
  102. } else {
  103. if (cdxl->video_stream_index == -1) {
  104. AVStream *st = avformat_new_stream(s, NULL);
  105. if (!st)
  106. return AVERROR(ENOMEM);
  107. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  108. st->codec->codec_tag = 0;
  109. st->codec->codec_id = AV_CODEC_ID_CDXL;
  110. st->codec->width = width;
  111. st->codec->height = height;
  112. st->start_time = 0;
  113. cdxl->video_stream_index = st->index;
  114. if (cdxl->framerate)
  115. avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
  116. else
  117. avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
  118. }
  119. if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
  120. return AVERROR(ENOMEM);
  121. memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
  122. ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
  123. if (ret < 0) {
  124. av_free_packet(pkt);
  125. return ret;
  126. }
  127. av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
  128. pkt->stream_index = cdxl->video_stream_index;
  129. pkt->flags |= AV_PKT_FLAG_KEY;
  130. pkt->pos = pos;
  131. pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
  132. cdxl->read_chunk = audio_size;
  133. }
  134. if (!cdxl->read_chunk)
  135. avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
  136. return ret;
  137. }
  138. #define OFFSET(x) offsetof(CDXLDemuxContext, x)
  139. static const AVOption cdxl_options[] = {
  140. { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  141. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  142. { NULL },
  143. };
  144. static const AVClass cdxl_demuxer_class = {
  145. .class_name = "CDXL demuxer",
  146. .item_name = av_default_item_name,
  147. .option = cdxl_options,
  148. .version = LIBAVUTIL_VERSION_INT,
  149. };
  150. AVInputFormat ff_cdxl_demuxer = {
  151. .name = "cdxl",
  152. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
  153. .priv_data_size = sizeof(CDXLDemuxContext),
  154. .read_header = cdxl_read_header,
  155. .read_packet = cdxl_read_packet,
  156. .extensions = "cdxl,xl",
  157. .flags = AVFMT_GENERIC_INDEX,
  158. .priv_class = &cdxl_demuxer_class,
  159. };