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
7.9KB

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