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.

345 lines
11KB

  1. /*
  2. * NuppelVideo demuxer.
  3. * Copyright (c) 2006 Reimar Doeffinger
  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 "libavutil/intfloat_readwrite.h"
  23. #include "avformat.h"
  24. #include "riff.h"
  25. typedef struct {
  26. int v_id;
  27. int a_id;
  28. int rtjpg_video;
  29. } NUVContext;
  30. typedef enum {
  31. NUV_VIDEO = 'V',
  32. NUV_EXTRADATA = 'D',
  33. NUV_AUDIO = 'A',
  34. NUV_SEEKP = 'R',
  35. NUV_MYTHEXT = 'X'
  36. } nuv_frametype;
  37. static int nuv_probe(AVProbeData *p) {
  38. if (!memcmp(p->buf, "NuppelVideo", 12))
  39. return AVPROBE_SCORE_MAX;
  40. if (!memcmp(p->buf, "MythTVVideo", 12))
  41. return AVPROBE_SCORE_MAX;
  42. return 0;
  43. }
  44. //! little macro to sanitize packet size
  45. #define PKTSIZE(s) (s & 0xffffff)
  46. /**
  47. * @brief read until we found all data needed for decoding
  48. * @param vst video stream of which to change parameters
  49. * @param ast video stream of which to change parameters
  50. * @param myth set if this is a MythTVVideo format file
  51. * @return 1 if all required codec data was found
  52. */
  53. static int get_codec_data(AVIOContext *pb, AVStream *vst,
  54. AVStream *ast, int myth) {
  55. nuv_frametype frametype;
  56. if (!vst && !myth)
  57. return 1; // no codec data needed
  58. while (!url_feof(pb)) {
  59. int size, subtype;
  60. frametype = avio_r8(pb);
  61. switch (frametype) {
  62. case NUV_EXTRADATA:
  63. subtype = avio_r8(pb);
  64. avio_skip(pb, 6);
  65. size = PKTSIZE(avio_rl32(pb));
  66. if (vst && subtype == 'R') {
  67. vst->codec->extradata_size = size;
  68. vst->codec->extradata = av_malloc(size);
  69. avio_read(pb, vst->codec->extradata, size);
  70. size = 0;
  71. if (!myth)
  72. return 1;
  73. }
  74. break;
  75. case NUV_MYTHEXT:
  76. avio_skip(pb, 7);
  77. size = PKTSIZE(avio_rl32(pb));
  78. if (size != 128 * 4)
  79. break;
  80. avio_rl32(pb); // version
  81. if (vst) {
  82. vst->codec->codec_tag = avio_rl32(pb);
  83. vst->codec->codec_id =
  84. ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
  85. if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
  86. vst->codec->codec_id = CODEC_ID_NUV;
  87. } else
  88. avio_skip(pb, 4);
  89. if (ast) {
  90. ast->codec->codec_tag = avio_rl32(pb);
  91. ast->codec->sample_rate = avio_rl32(pb);
  92. ast->codec->bits_per_coded_sample = avio_rl32(pb);
  93. ast->codec->channels = avio_rl32(pb);
  94. ast->codec->codec_id =
  95. ff_wav_codec_get_id(ast->codec->codec_tag,
  96. ast->codec->bits_per_coded_sample);
  97. ast->need_parsing = AVSTREAM_PARSE_FULL;
  98. } else
  99. avio_skip(pb, 4 * 4);
  100. size -= 6 * 4;
  101. avio_skip(pb, size);
  102. return 1;
  103. case NUV_SEEKP:
  104. size = 11;
  105. break;
  106. default:
  107. avio_skip(pb, 7);
  108. size = PKTSIZE(avio_rl32(pb));
  109. break;
  110. }
  111. avio_skip(pb, size);
  112. }
  113. return 0;
  114. }
  115. static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
  116. NUVContext *ctx = s->priv_data;
  117. AVIOContext *pb = s->pb;
  118. char id_string[12];
  119. double aspect, fps;
  120. int is_mythtv, width, height, v_packs, a_packs;
  121. int stream_nr = 0;
  122. AVStream *vst = NULL, *ast = NULL;
  123. avio_read(pb, id_string, 12);
  124. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  125. avio_skip(pb, 5); // version string
  126. avio_skip(pb, 3); // padding
  127. width = avio_rl32(pb);
  128. height = avio_rl32(pb);
  129. avio_rl32(pb); // unused, "desiredwidth"
  130. avio_rl32(pb); // unused, "desiredheight"
  131. avio_r8(pb); // 'P' == progressive, 'I' == interlaced
  132. avio_skip(pb, 3); // padding
  133. aspect = av_int2dbl(avio_rl64(pb));
  134. if (aspect > 0.9999 && aspect < 1.0001)
  135. aspect = 4.0 / 3.0;
  136. fps = av_int2dbl(avio_rl64(pb));
  137. // number of packets per stream type, -1 means unknown, e.g. streaming
  138. v_packs = avio_rl32(pb);
  139. a_packs = avio_rl32(pb);
  140. avio_rl32(pb); // text
  141. avio_rl32(pb); // keyframe distance (?)
  142. if (v_packs) {
  143. ctx->v_id = stream_nr++;
  144. vst = av_new_stream(s, ctx->v_id);
  145. if (!vst)
  146. return AVERROR(ENOMEM);
  147. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  148. vst->codec->codec_id = CODEC_ID_NUV;
  149. vst->codec->width = width;
  150. vst->codec->height = height;
  151. vst->codec->bits_per_coded_sample = 10;
  152. vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
  153. vst->r_frame_rate = av_d2q(fps, 60000);
  154. av_set_pts_info(vst, 32, 1, 1000);
  155. } else
  156. ctx->v_id = -1;
  157. if (a_packs) {
  158. ctx->a_id = stream_nr++;
  159. ast = av_new_stream(s, ctx->a_id);
  160. if (!ast)
  161. return AVERROR(ENOMEM);
  162. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  163. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  164. ast->codec->channels = 2;
  165. ast->codec->sample_rate = 44100;
  166. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  167. ast->codec->block_align = 2 * 2;
  168. ast->codec->bits_per_coded_sample = 16;
  169. av_set_pts_info(ast, 32, 1, 1000);
  170. } else
  171. ctx->a_id = -1;
  172. get_codec_data(pb, vst, ast, is_mythtv);
  173. ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
  174. return 0;
  175. }
  176. #define HDRSIZE 12
  177. static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
  178. NUVContext *ctx = s->priv_data;
  179. AVIOContext *pb = s->pb;
  180. uint8_t hdr[HDRSIZE];
  181. nuv_frametype frametype;
  182. int ret, size;
  183. while (!url_feof(pb)) {
  184. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  185. uint64_t pos = avio_tell(pb);
  186. ret = avio_read(pb, hdr, HDRSIZE);
  187. if (ret < HDRSIZE)
  188. return ret < 0 ? ret : AVERROR(EIO);
  189. frametype = hdr[0];
  190. size = PKTSIZE(AV_RL32(&hdr[8]));
  191. switch (frametype) {
  192. case NUV_EXTRADATA:
  193. if (!ctx->rtjpg_video) {
  194. avio_skip(pb, size);
  195. break;
  196. }
  197. case NUV_VIDEO:
  198. if (ctx->v_id < 0) {
  199. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  200. avio_skip(pb, size);
  201. break;
  202. }
  203. ret = av_new_packet(pkt, copyhdrsize + size);
  204. if (ret < 0)
  205. return ret;
  206. pkt->pos = pos;
  207. pkt->flags |= hdr[2] == 0 ? AV_PKT_FLAG_KEY : 0;
  208. pkt->pts = AV_RL32(&hdr[4]);
  209. pkt->stream_index = ctx->v_id;
  210. memcpy(pkt->data, hdr, copyhdrsize);
  211. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  212. if (ret < 0) {
  213. av_free_packet(pkt);
  214. return ret;
  215. }
  216. if (ret < size)
  217. av_shrink_packet(pkt, copyhdrsize + ret);
  218. return 0;
  219. case NUV_AUDIO:
  220. if (ctx->a_id < 0) {
  221. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  222. avio_skip(pb, size);
  223. break;
  224. }
  225. ret = av_get_packet(pb, pkt, size);
  226. pkt->flags |= AV_PKT_FLAG_KEY;
  227. pkt->pos = pos;
  228. pkt->pts = AV_RL32(&hdr[4]);
  229. pkt->stream_index = ctx->a_id;
  230. if (ret < 0) return ret;
  231. return 0;
  232. case NUV_SEEKP:
  233. // contains no data, size value is invalid
  234. break;
  235. default:
  236. avio_skip(pb, size);
  237. break;
  238. }
  239. }
  240. return AVERROR(EIO);
  241. }
  242. /**
  243. * \brief looks for the string RTjjjjjjjjjj in the stream too resync reading
  244. * \return 1 if the syncword is found 0 otherwise.
  245. */
  246. static int nuv_resync(AVFormatContext *s, int64_t pos_limit) {
  247. AVIOContext *pb = s->pb;
  248. uint32_t tag = 0;
  249. while(!url_feof(pb) && avio_tell(pb) < pos_limit) {
  250. tag = (tag << 8) | avio_r8(pb);
  251. if (tag == MKBETAG('R','T','j','j') &&
  252. (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j') &&
  253. (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j'))
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * \brief attempts to read a timestamp from stream at the given stream position
  260. * \return timestamp if successfull and AV_NOPTS_VALUE if failure
  261. */
  262. static int64_t nuv_read_dts(AVFormatContext *s, int stream_index,
  263. int64_t *ppos, int64_t pos_limit)
  264. {
  265. NUVContext *ctx = s->priv_data;
  266. AVIOContext *pb = s->pb;
  267. uint8_t hdr[HDRSIZE];
  268. nuv_frametype frametype;
  269. int size, key, idx;
  270. int64_t pos, dts;
  271. if (avio_seek(pb, *ppos, SEEK_SET) < 0)
  272. return AV_NOPTS_VALUE;
  273. if (!nuv_resync(s, pos_limit))
  274. return AV_NOPTS_VALUE;
  275. while (!url_feof(pb) && avio_tell(pb) < pos_limit) {
  276. if (avio_read(pb, hdr, HDRSIZE) < HDRSIZE)
  277. return AV_NOPTS_VALUE;
  278. frametype = hdr[0];
  279. size = PKTSIZE(AV_RL32(&hdr[8]));
  280. switch (frametype) {
  281. case NUV_SEEKP:
  282. break;
  283. case NUV_AUDIO:
  284. case NUV_VIDEO:
  285. if (frametype == NUV_VIDEO) {
  286. idx = ctx->v_id;
  287. key = hdr[2] == 0;
  288. } else {
  289. idx = ctx->a_id;
  290. key = 1;
  291. }
  292. if (stream_index == idx) {
  293. pos = avio_tell(s->pb) - HDRSIZE;
  294. dts = AV_RL32(&hdr[4]);
  295. // TODO - add general support in av_gen_search, so it adds positions after reading timestamps
  296. av_add_index_entry(s->streams[stream_index], pos, dts, size + HDRSIZE, 0,
  297. key ? AVINDEX_KEYFRAME : 0);
  298. *ppos = pos;
  299. return dts;
  300. }
  301. default:
  302. avio_skip(pb, size);
  303. break;
  304. }
  305. }
  306. return AV_NOPTS_VALUE;
  307. }
  308. AVInputFormat ff_nuv_demuxer = {
  309. .name = "nuv",
  310. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo format"),
  311. .priv_data_size = sizeof(NUVContext),
  312. .read_probe = nuv_probe,
  313. .read_header = nuv_header,
  314. .read_packet = nuv_packet,
  315. .read_timestamp = nuv_read_dts,
  316. .flags = AVFMT_GENERIC_INDEX,
  317. };