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.

350 lines
11KB

  1. /*
  2. * LXF demuxer
  3. * Copyright (c) 2010 Tomas Härdin
  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/intreadwrite.h"
  22. #include "libavcodec/bytestream.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "riff.h"
  26. #define LXF_MAX_PACKET_HEADER_SIZE 256
  27. #define LXF_HEADER_DATA_SIZE 120
  28. #define LXF_IDENT "LEITCH\0"
  29. #define LXF_IDENT_LENGTH 8
  30. #define LXF_SAMPLERATE 48000
  31. #define LXF_MAX_AUDIO_PACKET (8008*15*4) ///< 15-channel 32-bit NTSC audio frame
  32. static const AVCodecTag lxf_tags[] = {
  33. { AV_CODEC_ID_MJPEG, 0 },
  34. { AV_CODEC_ID_MPEG1VIDEO, 1 },
  35. { AV_CODEC_ID_MPEG2VIDEO, 2 }, //MpMl, 4:2:0
  36. { AV_CODEC_ID_MPEG2VIDEO, 3 }, //MpPl, 4:2:2
  37. { AV_CODEC_ID_DVVIDEO, 4 }, //DV25
  38. { AV_CODEC_ID_DVVIDEO, 5 }, //DVCPRO
  39. { AV_CODEC_ID_DVVIDEO, 6 }, //DVCPRO50
  40. { AV_CODEC_ID_RAWVIDEO, 7 }, //AV_PIX_FMT_ARGB, where alpha is used for chroma keying
  41. { AV_CODEC_ID_RAWVIDEO, 8 }, //16-bit chroma key
  42. { AV_CODEC_ID_MPEG2VIDEO, 9 }, //4:2:2 CBP ("Constrained Bytes per Gop")
  43. { AV_CODEC_ID_NONE, 0 },
  44. };
  45. typedef struct {
  46. int channels; ///< number of audio channels. zero means no audio
  47. int frame_number; ///< current video frame
  48. uint32_t video_format, packet_type, extended_size;
  49. } LXFDemuxContext;
  50. static int lxf_probe(AVProbeData *p)
  51. {
  52. if (!memcmp(p->buf, LXF_IDENT, LXF_IDENT_LENGTH))
  53. return AVPROBE_SCORE_MAX;
  54. return 0;
  55. }
  56. /**
  57. * Verify the checksum of an LXF packet header
  58. *
  59. * @param[in] header the packet header to check
  60. * @return zero if the checksum is OK, non-zero otherwise
  61. */
  62. static int check_checksum(const uint8_t *header, int size)
  63. {
  64. int x;
  65. uint32_t sum = 0;
  66. for (x = 0; x < size; x += 4)
  67. sum += AV_RL32(&header[x]);
  68. return sum;
  69. }
  70. /**
  71. * Read input until we find the next ident. If found, copy it to the header buffer
  72. *
  73. * @param[out] header where to copy the ident to
  74. * @return 0 if an ident was found, < 0 on I/O error
  75. */
  76. static int sync(AVFormatContext *s, uint8_t *header)
  77. {
  78. uint8_t buf[LXF_IDENT_LENGTH];
  79. int ret;
  80. if ((ret = avio_read(s->pb, buf, LXF_IDENT_LENGTH)) != LXF_IDENT_LENGTH)
  81. return ret < 0 ? ret : AVERROR_EOF;
  82. while (memcmp(buf, LXF_IDENT, LXF_IDENT_LENGTH)) {
  83. if (url_feof(s->pb))
  84. return AVERROR_EOF;
  85. memmove(buf, &buf[1], LXF_IDENT_LENGTH-1);
  86. buf[LXF_IDENT_LENGTH-1] = avio_r8(s->pb);
  87. }
  88. memcpy(header, LXF_IDENT, LXF_IDENT_LENGTH);
  89. return 0;
  90. }
  91. /**
  92. * Read and checksum the next packet header
  93. *
  94. * @return the size of the payload following the header or < 0 on failure
  95. */
  96. static int get_packet_header(AVFormatContext *s)
  97. {
  98. LXFDemuxContext *lxf = s->priv_data;
  99. AVIOContext *pb = s->pb;
  100. int track_size, samples, ret;
  101. uint32_t version, audio_format, header_size, channels, tmp;
  102. AVStream *st;
  103. uint8_t header[LXF_MAX_PACKET_HEADER_SIZE];
  104. const uint8_t *p;
  105. //find and read the ident
  106. if ((ret = sync(s, header)) < 0)
  107. return ret;
  108. ret = avio_read(pb, header + LXF_IDENT_LENGTH, 8);
  109. if (ret != 8)
  110. return ret < 0 ? ret : AVERROR_EOF;
  111. p = header + LXF_IDENT_LENGTH;
  112. version = bytestream_get_le32(&p);
  113. header_size = bytestream_get_le32(&p);
  114. if (version > 1)
  115. av_log_ask_for_sample(s, "Unknown format version %i\n", version);
  116. if (header_size < (version ? 72 : 60) ||
  117. header_size > LXF_MAX_PACKET_HEADER_SIZE ||
  118. (header_size & 3)) {
  119. av_log(s, AV_LOG_ERROR, "Invalid header size 0x%x\n", header_size);
  120. return AVERROR_INVALIDDATA;
  121. }
  122. //read the rest of the packet header
  123. if ((ret = avio_read(pb, header + (p - header),
  124. header_size - (p - header))) !=
  125. header_size - (p - header)) {
  126. return ret < 0 ? ret : AVERROR_EOF;
  127. }
  128. if (check_checksum(header, header_size))
  129. av_log(s, AV_LOG_ERROR, "checksum error\n");
  130. lxf->packet_type = bytestream_get_le32(&p);
  131. p += version ? 20 : 12;
  132. lxf->extended_size = 0;
  133. switch (lxf->packet_type) {
  134. case 0:
  135. //video
  136. lxf->video_format = bytestream_get_le32(&p);
  137. ret = bytestream_get_le32(&p);
  138. //skip VBI data and metadata
  139. avio_skip(pb, (int64_t)(uint32_t)AV_RL32(p + 4) +
  140. (int64_t)(uint32_t)AV_RL32(p + 12));
  141. break;
  142. case 1:
  143. //audio
  144. if (!s->streams || !(st = s->streams[1])) {
  145. av_log(s, AV_LOG_INFO, "got audio packet, but no audio stream present\n");
  146. break;
  147. }
  148. if (version == 0) p += 8;
  149. audio_format = bytestream_get_le32(&p);
  150. channels = bytestream_get_le32(&p);
  151. track_size = bytestream_get_le32(&p);
  152. //set codec based on specified audio bitdepth
  153. //we only support tightly packed 16-, 20-, 24- and 32-bit PCM at the moment
  154. st->codec->bits_per_coded_sample = (audio_format >> 6) & 0x3F;
  155. if (st->codec->bits_per_coded_sample != (audio_format & 0x3F)) {
  156. av_log(s, AV_LOG_WARNING, "only tightly packed PCM currently supported\n");
  157. return AVERROR_PATCHWELCOME;
  158. }
  159. switch (st->codec->bits_per_coded_sample) {
  160. case 16: st->codec->codec_id = AV_CODEC_ID_PCM_S16LE_PLANAR; break;
  161. case 20: st->codec->codec_id = AV_CODEC_ID_PCM_LXF; break;
  162. case 24: st->codec->codec_id = AV_CODEC_ID_PCM_S24LE_PLANAR; break;
  163. case 32: st->codec->codec_id = AV_CODEC_ID_PCM_S32LE_PLANAR; break;
  164. default:
  165. av_log(s, AV_LOG_WARNING,
  166. "only 16-, 20-, 24- and 32-bit PCM currently supported\n");
  167. return AVERROR_PATCHWELCOME;
  168. }
  169. samples = track_size * 8 / st->codec->bits_per_coded_sample;
  170. //use audio packet size to determine video standard
  171. //for NTSC we have one 8008-sample audio frame per five video frames
  172. if (samples == LXF_SAMPLERATE * 5005 / 30000) {
  173. avpriv_set_pts_info(s->streams[0], 64, 1001, 30000);
  174. } else {
  175. //assume PAL, but warn if we don't have 1920 samples
  176. if (samples != LXF_SAMPLERATE / 25)
  177. av_log(s, AV_LOG_WARNING,
  178. "video doesn't seem to be PAL or NTSC. guessing PAL\n");
  179. avpriv_set_pts_info(s->streams[0], 64, 1, 25);
  180. }
  181. //TODO: warning if track mask != (1 << channels) - 1?
  182. ret = av_popcount(channels) * track_size;
  183. break;
  184. default:
  185. tmp = bytestream_get_le32(&p);
  186. ret = bytestream_get_le32(&p);
  187. if (tmp == 1)
  188. lxf->extended_size = bytestream_get_le32(&p);
  189. break;
  190. }
  191. return ret;
  192. }
  193. static int lxf_read_header(AVFormatContext *s)
  194. {
  195. LXFDemuxContext *lxf = s->priv_data;
  196. AVIOContext *pb = s->pb;
  197. uint8_t header_data[LXF_HEADER_DATA_SIZE];
  198. int ret;
  199. AVStream *st;
  200. uint32_t video_params, disk_params;
  201. uint16_t record_date, expiration_date;
  202. if ((ret = get_packet_header(s)) < 0)
  203. return ret;
  204. if (ret != LXF_HEADER_DATA_SIZE) {
  205. av_log(s, AV_LOG_ERROR, "expected %d B size header, got %d\n",
  206. LXF_HEADER_DATA_SIZE, ret);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. if ((ret = avio_read(pb, header_data, LXF_HEADER_DATA_SIZE)) != LXF_HEADER_DATA_SIZE)
  210. return ret < 0 ? ret : AVERROR_EOF;
  211. if (!(st = avformat_new_stream(s, NULL)))
  212. return AVERROR(ENOMEM);
  213. st->duration = AV_RL32(&header_data[32]);
  214. video_params = AV_RL32(&header_data[40]);
  215. record_date = AV_RL16(&header_data[56]);
  216. expiration_date = AV_RL16(&header_data[58]);
  217. disk_params = AV_RL32(&header_data[116]);
  218. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  219. st->codec->bit_rate = 1000000 * ((video_params >> 14) & 0xFF);
  220. st->codec->codec_tag = video_params & 0xF;
  221. st->codec->codec_id = ff_codec_get_id(lxf_tags, st->codec->codec_tag);
  222. av_log(s, AV_LOG_DEBUG, "record: %x = %i-%02i-%02i\n",
  223. record_date, 1900 + (record_date & 0x7F), (record_date >> 7) & 0xF,
  224. (record_date >> 11) & 0x1F);
  225. av_log(s, AV_LOG_DEBUG, "expire: %x = %i-%02i-%02i\n",
  226. expiration_date, 1900 + (expiration_date & 0x7F), (expiration_date >> 7) & 0xF,
  227. (expiration_date >> 11) & 0x1F);
  228. if ((video_params >> 22) & 1)
  229. av_log(s, AV_LOG_WARNING, "VBI data not yet supported\n");
  230. if ((lxf->channels = 1 << (disk_params >> 4 & 3) + 1)) {
  231. if (!(st = avformat_new_stream(s, NULL)))
  232. return AVERROR(ENOMEM);
  233. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  234. st->codec->sample_rate = LXF_SAMPLERATE;
  235. st->codec->channels = lxf->channels;
  236. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  237. }
  238. avio_skip(s->pb, lxf->extended_size);
  239. return 0;
  240. }
  241. static int lxf_read_packet(AVFormatContext *s, AVPacket *pkt)
  242. {
  243. LXFDemuxContext *lxf = s->priv_data;
  244. AVIOContext *pb = s->pb;
  245. AVStream *ast = NULL;
  246. uint32_t stream;
  247. int ret, ret2;
  248. if ((ret = get_packet_header(s)) < 0)
  249. return ret;
  250. stream = lxf->packet_type;
  251. if (stream > 1) {
  252. av_log(s, AV_LOG_WARNING, "got packet with illegal stream index %u\n", stream);
  253. return AVERROR(EAGAIN);
  254. }
  255. if (stream == 1 && !(ast = s->streams[1])) {
  256. av_log(s, AV_LOG_ERROR, "got audio packet without having an audio stream\n");
  257. return AVERROR_INVALIDDATA;
  258. }
  259. //make sure the data fits in the de-planerization buffer
  260. if (ast && ret > LXF_MAX_AUDIO_PACKET) {
  261. av_log(s, AV_LOG_ERROR, "audio packet too large (%i > %i)\n",
  262. ret, LXF_MAX_AUDIO_PACKET);
  263. return AVERROR_INVALIDDATA;
  264. }
  265. if ((ret2 = av_new_packet(pkt, ret)) < 0)
  266. return ret2;
  267. if ((ret2 = avio_read(pb, pkt->data, ret)) != ret) {
  268. av_free_packet(pkt);
  269. return ret2 < 0 ? ret2 : AVERROR_EOF;
  270. }
  271. pkt->stream_index = stream;
  272. if (!ast) {
  273. //picture type (0 = closed I, 1 = open I, 2 = P, 3 = B)
  274. if (((lxf->video_format >> 22) & 0x3) < 2)
  275. pkt->flags |= AV_PKT_FLAG_KEY;
  276. pkt->dts = lxf->frame_number++;
  277. }
  278. return ret;
  279. }
  280. AVInputFormat ff_lxf_demuxer = {
  281. .name = "lxf",
  282. .long_name = NULL_IF_CONFIG_SMALL("VR native stream (LXF)"),
  283. .priv_data_size = sizeof(LXFDemuxContext),
  284. .read_probe = lxf_probe,
  285. .read_header = lxf_read_header,
  286. .read_packet = lxf_read_packet,
  287. .codec_tag = (const AVCodecTag* const []){lxf_tags, 0},
  288. };