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.

403 lines
13KB

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