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.

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