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.

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