lxfdec.c 11 KB

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