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.

225 lines
7.1KB

  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_probe(AVProbeData *p)
  38. {
  39. int score = AVPROBE_SCORE_MAX / 2 + 10;
  40. if (p->buf_size < CDXL_HEADER_SIZE)
  41. return 0;
  42. /* reserved bytes should always be set to 0 */
  43. if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
  44. return 0;
  45. /* check type */
  46. if (p->buf[0] != 1)
  47. return 0;
  48. /* check palette size */
  49. if (AV_RB16(&p->buf[20]) > 512)
  50. return 0;
  51. /* check number of planes */
  52. if (p->buf[18] || !p->buf[19])
  53. return 0;
  54. /* check widh and height */
  55. if (!AV_RN16(&p->buf[14]) || !AV_RN16(&p->buf[16]))
  56. return 0;
  57. /* chunk size */
  58. if (AV_RB32(&p->buf[2]) < AV_RB16(&p->buf[22]) + AV_RB16(&p->buf[20]) + CDXL_HEADER_SIZE)
  59. return 0;
  60. /* previous chunk size */
  61. if (AV_RN32(&p->buf[6]))
  62. score /= 2;
  63. /* current frame number, usually starts from 1 */
  64. if (AV_RB16(&p->buf[12]) != 1)
  65. score /= 2;
  66. return score;
  67. }
  68. static int cdxl_read_header(AVFormatContext *s)
  69. {
  70. CDXLDemuxContext *cdxl = s->priv_data;
  71. int ret;
  72. if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
  73. av_log(s, AV_LOG_ERROR,
  74. "Could not parse framerate: %s.\n", cdxl->framerate);
  75. return ret;
  76. }
  77. cdxl->read_chunk = 0;
  78. cdxl->video_stream_index = -1;
  79. cdxl->audio_stream_index = -1;
  80. s->ctx_flags |= AVFMTCTX_NOHEADER;
  81. return 0;
  82. }
  83. static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
  84. {
  85. CDXLDemuxContext *cdxl = s->priv_data;
  86. AVIOContext *pb = s->pb;
  87. uint32_t current_size, video_size, image_size;
  88. uint16_t audio_size, palette_size, width, height;
  89. int64_t pos;
  90. int ret;
  91. if (pb->eof_reached)
  92. return AVERROR_EOF;
  93. pos = avio_tell(pb);
  94. if (!cdxl->read_chunk &&
  95. avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
  96. return AVERROR_EOF;
  97. if (cdxl->header[0] != 1) {
  98. av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. current_size = AV_RB32(&cdxl->header[2]);
  102. width = AV_RB16(&cdxl->header[14]);
  103. height = AV_RB16(&cdxl->header[16]);
  104. palette_size = AV_RB16(&cdxl->header[20]);
  105. audio_size = AV_RB16(&cdxl->header[22]);
  106. image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
  107. video_size = palette_size + image_size;
  108. if (palette_size > 512)
  109. return AVERROR_INVALIDDATA;
  110. if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
  111. return AVERROR_INVALIDDATA;
  112. if (cdxl->read_chunk && audio_size) {
  113. if (cdxl->audio_stream_index == -1) {
  114. AVStream *st = avformat_new_stream(s, NULL);
  115. if (!st)
  116. return AVERROR(ENOMEM);
  117. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  118. st->codec->codec_tag = 0;
  119. st->codec->codec_id = AV_CODEC_ID_PCM_S8;
  120. st->codec->channels = cdxl->header[1] & 0x10 ? 2 : 1;
  121. st->codec->sample_rate = cdxl->sample_rate;
  122. st->start_time = 0;
  123. cdxl->audio_stream_index = st->index;
  124. avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
  125. }
  126. ret = av_get_packet(pb, pkt, audio_size);
  127. if (ret < 0)
  128. return ret;
  129. pkt->stream_index = cdxl->audio_stream_index;
  130. pkt->pos = pos;
  131. pkt->duration = audio_size;
  132. cdxl->read_chunk = 0;
  133. } else {
  134. if (cdxl->video_stream_index == -1) {
  135. AVStream *st = avformat_new_stream(s, NULL);
  136. if (!st)
  137. return AVERROR(ENOMEM);
  138. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  139. st->codec->codec_tag = 0;
  140. st->codec->codec_id = AV_CODEC_ID_CDXL;
  141. st->codec->width = width;
  142. st->codec->height = height;
  143. st->start_time = 0;
  144. cdxl->video_stream_index = st->index;
  145. if (cdxl->framerate)
  146. avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
  147. else
  148. avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
  149. }
  150. if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
  151. return AVERROR(ENOMEM);
  152. memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
  153. ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
  154. if (ret < 0) {
  155. av_free_packet(pkt);
  156. return ret;
  157. }
  158. av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
  159. pkt->stream_index = cdxl->video_stream_index;
  160. pkt->flags |= AV_PKT_FLAG_KEY;
  161. pkt->pos = pos;
  162. pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
  163. cdxl->read_chunk = audio_size;
  164. }
  165. if (!cdxl->read_chunk)
  166. avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
  167. return ret;
  168. }
  169. #define OFFSET(x) offsetof(CDXLDemuxContext, x)
  170. static const AVOption cdxl_options[] = {
  171. { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  172. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  173. { NULL },
  174. };
  175. static const AVClass cdxl_demuxer_class = {
  176. .class_name = "CDXL demuxer",
  177. .item_name = av_default_item_name,
  178. .option = cdxl_options,
  179. .version = LIBAVUTIL_VERSION_INT,
  180. };
  181. AVInputFormat ff_cdxl_demuxer = {
  182. .name = "cdxl",
  183. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
  184. .priv_data_size = sizeof(CDXLDemuxContext),
  185. .read_probe = cdxl_read_probe,
  186. .read_header = cdxl_read_header,
  187. .read_packet = cdxl_read_packet,
  188. .extensions = "cdxl,xl",
  189. .flags = AVFMT_GENERIC_INDEX,
  190. .priv_class = &cdxl_demuxer_class,
  191. };