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.

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