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.

232 lines
7.4KB

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