nuv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 = 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, AVFormatParameters *ap) {
  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 = 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. vst->r_frame_rate = av_d2q(fps, 60000);
  155. avpriv_set_pts_info(vst, 32, 1, 1000);
  156. } else
  157. ctx->v_id = -1;
  158. if (a_packs) {
  159. ctx->a_id = stream_nr++;
  160. ast = avformat_new_stream(s, NULL);
  161. if (!ast)
  162. return AVERROR(ENOMEM);
  163. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  164. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  165. ast->codec->channels = 2;
  166. ast->codec->sample_rate = 44100;
  167. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  168. ast->codec->block_align = 2 * 2;
  169. ast->codec->bits_per_coded_sample = 16;
  170. avpriv_set_pts_info(ast, 32, 1, 1000);
  171. } else
  172. ctx->a_id = -1;
  173. get_codec_data(pb, vst, ast, is_mythtv);
  174. ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
  175. return 0;
  176. }
  177. #define HDRSIZE 12
  178. static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
  179. NUVContext *ctx = s->priv_data;
  180. AVIOContext *pb = s->pb;
  181. uint8_t hdr[HDRSIZE];
  182. nuv_frametype frametype;
  183. int ret, size;
  184. while (!url_feof(pb)) {
  185. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  186. uint64_t pos = avio_tell(pb);
  187. ret = avio_read(pb, hdr, HDRSIZE);
  188. if (ret < HDRSIZE)
  189. return ret < 0 ? ret : AVERROR(EIO);
  190. frametype = hdr[0];
  191. size = PKTSIZE(AV_RL32(&hdr[8]));
  192. switch (frametype) {
  193. case NUV_EXTRADATA:
  194. if (!ctx->rtjpg_video) {
  195. avio_skip(pb, size);
  196. break;
  197. }
  198. case NUV_VIDEO:
  199. if (ctx->v_id < 0) {
  200. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  201. avio_skip(pb, size);
  202. break;
  203. }
  204. ret = av_new_packet(pkt, copyhdrsize + size);
  205. if (ret < 0)
  206. return ret;
  207. pkt->pos = pos;
  208. pkt->flags |= hdr[2] == 0 ? AV_PKT_FLAG_KEY : 0;
  209. pkt->pts = AV_RL32(&hdr[4]);
  210. pkt->stream_index = ctx->v_id;
  211. memcpy(pkt->data, hdr, copyhdrsize);
  212. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  213. if (ret < 0) {
  214. av_free_packet(pkt);
  215. return ret;
  216. }
  217. if (ret < size)
  218. av_shrink_packet(pkt, copyhdrsize + ret);
  219. return 0;
  220. case NUV_AUDIO:
  221. if (ctx->a_id < 0) {
  222. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  223. avio_skip(pb, size);
  224. break;
  225. }
  226. ret = av_get_packet(pb, pkt, size);
  227. pkt->flags |= AV_PKT_FLAG_KEY;
  228. pkt->pos = pos;
  229. pkt->pts = AV_RL32(&hdr[4]);
  230. pkt->stream_index = ctx->a_id;
  231. if (ret < 0) return ret;
  232. return 0;
  233. case NUV_SEEKP:
  234. // contains no data, size value is invalid
  235. break;
  236. default:
  237. avio_skip(pb, size);
  238. break;
  239. }
  240. }
  241. return AVERROR(EIO);
  242. }
  243. /**
  244. * \brief looks for the string RTjjjjjjjjjj in the stream too resync reading
  245. * \return 1 if the syncword is found 0 otherwise.
  246. */
  247. static int nuv_resync(AVFormatContext *s, int64_t pos_limit) {
  248. AVIOContext *pb = s->pb;
  249. uint32_t tag = 0;
  250. while(!url_feof(pb) && avio_tell(pb) < pos_limit) {
  251. tag = (tag << 8) | avio_r8(pb);
  252. if (tag == MKBETAG('R','T','j','j') &&
  253. (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j') &&
  254. (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j'))
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * \brief attempts to read a timestamp from stream at the given stream position
  261. * \return timestamp if successfull and AV_NOPTS_VALUE if failure
  262. */
  263. static int64_t nuv_read_dts(AVFormatContext *s, int stream_index,
  264. int64_t *ppos, int64_t pos_limit)
  265. {
  266. NUVContext *ctx = s->priv_data;
  267. AVIOContext *pb = s->pb;
  268. uint8_t hdr[HDRSIZE];
  269. nuv_frametype frametype;
  270. int size, key, idx;
  271. int64_t pos, dts;
  272. if (avio_seek(pb, *ppos, SEEK_SET) < 0)
  273. return AV_NOPTS_VALUE;
  274. if (!nuv_resync(s, pos_limit))
  275. return AV_NOPTS_VALUE;
  276. while (!url_feof(pb) && avio_tell(pb) < pos_limit) {
  277. if (avio_read(pb, hdr, HDRSIZE) < HDRSIZE)
  278. return AV_NOPTS_VALUE;
  279. frametype = hdr[0];
  280. size = PKTSIZE(AV_RL32(&hdr[8]));
  281. switch (frametype) {
  282. case NUV_SEEKP:
  283. break;
  284. case NUV_AUDIO:
  285. case NUV_VIDEO:
  286. if (frametype == NUV_VIDEO) {
  287. idx = ctx->v_id;
  288. key = hdr[2] == 0;
  289. } else {
  290. idx = ctx->a_id;
  291. key = 1;
  292. }
  293. if (stream_index == idx) {
  294. pos = avio_tell(s->pb) - HDRSIZE;
  295. dts = AV_RL32(&hdr[4]);
  296. // TODO - add general support in av_gen_search, so it adds positions after reading timestamps
  297. av_add_index_entry(s->streams[stream_index], pos, dts, size + HDRSIZE, 0,
  298. key ? AVINDEX_KEYFRAME : 0);
  299. *ppos = pos;
  300. return dts;
  301. }
  302. default:
  303. avio_skip(pb, size);
  304. break;
  305. }
  306. }
  307. return AV_NOPTS_VALUE;
  308. }
  309. AVInputFormat ff_nuv_demuxer = {
  310. .name = "nuv",
  311. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo format"),
  312. .priv_data_size = sizeof(NUVContext),
  313. .read_probe = nuv_probe,
  314. .read_header = nuv_header,
  315. .read_packet = nuv_packet,
  316. .read_timestamp = nuv_read_dts,
  317. .flags = AVFMT_GENERIC_INDEX,
  318. };