lxfdec.c 12 KB

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