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.

254 lines
8.3KB

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