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.

352 lines
12KB

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