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.

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