nuv.c 11 KB

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