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.

250 lines
8.0KB

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