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.

329 lines
11KB

  1. /*
  2. * NuppelVideo demuxer.
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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(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(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 (!pb->eof_reached) {
  69. int size, subtype;
  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 (vst->codecpar->extradata) {
  78. av_freep(&vst->codecpar->extradata);
  79. vst->codecpar->extradata_size = 0;
  80. }
  81. vst->codecpar->extradata = av_malloc(size);
  82. if (!vst->codecpar->extradata)
  83. return AVERROR(ENOMEM);
  84. vst->codecpar->extradata_size = size;
  85. avio_read(pb, vst->codecpar->extradata, size);
  86. size = 0;
  87. if (!myth)
  88. return 0;
  89. }
  90. break;
  91. case NUV_MYTHEXT:
  92. avio_skip(pb, 7);
  93. size = PKTSIZE(avio_rl32(pb));
  94. if (size != 128 * 4)
  95. break;
  96. avio_rl32(pb); // version
  97. if (vst) {
  98. vst->codecpar->codec_tag = avio_rl32(pb);
  99. vst->codecpar->codec_id =
  100. ff_codec_get_id(ff_codec_bmp_tags, vst->codecpar->codec_tag);
  101. if (vst->codecpar->codec_tag == MKTAG('R', 'J', 'P', 'G'))
  102. vst->codecpar->codec_id = AV_CODEC_ID_NUV;
  103. } else
  104. avio_skip(pb, 4);
  105. if (ast) {
  106. int id;
  107. ast->codecpar->codec_tag = avio_rl32(pb);
  108. ast->codecpar->sample_rate = avio_rl32(pb);
  109. ast->codecpar->bits_per_coded_sample = avio_rl32(pb);
  110. ast->codecpar->channels = avio_rl32(pb);
  111. ast->codecpar->channel_layout = 0;
  112. id = ff_wav_codec_get_id(ast->codecpar->codec_tag,
  113. ast->codecpar->bits_per_coded_sample);
  114. if (id == AV_CODEC_ID_NONE) {
  115. id = ff_codec_get_id(nuv_audio_tags, ast->codecpar->codec_tag);
  116. if (id == AV_CODEC_ID_PCM_S16LE)
  117. id = ff_get_pcm_codec_id(ast->codecpar->bits_per_coded_sample,
  118. 0, 0, ~1);
  119. }
  120. ast->codecpar->codec_id = id;
  121. ast->need_parsing = AVSTREAM_PARSE_FULL;
  122. } else
  123. avio_skip(pb, 4 * 4);
  124. size -= 6 * 4;
  125. avio_skip(pb, size);
  126. return 0;
  127. case NUV_SEEKP:
  128. size = 11;
  129. break;
  130. default:
  131. avio_skip(pb, 7);
  132. size = PKTSIZE(avio_rl32(pb));
  133. break;
  134. }
  135. avio_skip(pb, size);
  136. }
  137. return 0;
  138. }
  139. static int nuv_header(AVFormatContext *s)
  140. {
  141. NUVContext *ctx = s->priv_data;
  142. AVIOContext *pb = s->pb;
  143. char id_string[12];
  144. double aspect, fps;
  145. int is_mythtv, width, height, v_packs, a_packs, ret;
  146. AVStream *vst = NULL, *ast = NULL;
  147. avio_read(pb, id_string, 12);
  148. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  149. avio_skip(pb, 5); // version string
  150. avio_skip(pb, 3); // padding
  151. width = avio_rl32(pb);
  152. height = avio_rl32(pb);
  153. avio_rl32(pb); // unused, "desiredwidth"
  154. avio_rl32(pb); // unused, "desiredheight"
  155. avio_r8(pb); // 'P' == progressive, 'I' == interlaced
  156. avio_skip(pb, 3); // padding
  157. aspect = av_int2double(avio_rl64(pb));
  158. if (aspect > 0.9999 && aspect < 1.0001)
  159. aspect = 4.0 / 3.0;
  160. fps = av_int2double(avio_rl64(pb));
  161. if (fps < 0.0f) {
  162. if (s->error_recognition & AV_EF_EXPLODE) {
  163. av_log(s, AV_LOG_ERROR, "Invalid frame rate %f\n", fps);
  164. return AVERROR_INVALIDDATA;
  165. } else {
  166. av_log(s, AV_LOG_WARNING, "Invalid frame rate %f, setting to 0.\n", fps);
  167. fps = 0.0f;
  168. }
  169. }
  170. // number of packets per stream type, -1 means unknown, e.g. streaming
  171. v_packs = avio_rl32(pb);
  172. a_packs = avio_rl32(pb);
  173. avio_rl32(pb); // text
  174. avio_rl32(pb); // keyframe distance (?)
  175. if (v_packs) {
  176. vst = avformat_new_stream(s, NULL);
  177. if (!vst)
  178. return AVERROR(ENOMEM);
  179. ctx->v_id = vst->index;
  180. ret = av_image_check_size(width, height, 0, s);
  181. if (ret < 0)
  182. return ret;
  183. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  184. vst->codecpar->codec_id = AV_CODEC_ID_NUV;
  185. vst->codecpar->width = width;
  186. vst->codecpar->height = height;
  187. vst->codecpar->bits_per_coded_sample = 10;
  188. vst->sample_aspect_ratio = av_d2q(aspect * height / width,
  189. 10000);
  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(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 (!pb->eof_reached) {
  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. // HACK: we have no idea if it is a keyframe,
  247. // but if we mark none seeking will not work at all.
  248. pkt->flags |= AV_PKT_FLAG_KEY;
  249. pkt->pos = pos;
  250. pkt->pts = AV_RL32(&hdr[4]);
  251. pkt->stream_index = ctx->v_id;
  252. memcpy(pkt->data, hdr, copyhdrsize);
  253. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  254. if (ret < 0) {
  255. av_packet_unref(pkt);
  256. return ret;
  257. }
  258. if (ret < size)
  259. av_shrink_packet(pkt, copyhdrsize + ret);
  260. return 0;
  261. case NUV_AUDIO:
  262. if (ctx->a_id < 0) {
  263. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  264. avio_skip(pb, size);
  265. break;
  266. }
  267. ret = av_get_packet(pb, pkt, size);
  268. pkt->flags |= AV_PKT_FLAG_KEY;
  269. pkt->pos = pos;
  270. pkt->pts = AV_RL32(&hdr[4]);
  271. pkt->stream_index = ctx->a_id;
  272. if (ret < 0)
  273. return ret;
  274. return 0;
  275. case NUV_SEEKP:
  276. // contains no data, size value is invalid
  277. break;
  278. default:
  279. avio_skip(pb, size);
  280. break;
  281. }
  282. }
  283. return AVERROR(EIO);
  284. }
  285. AVInputFormat ff_nuv_demuxer = {
  286. .name = "nuv",
  287. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo"),
  288. .priv_data_size = sizeof(NUVContext),
  289. .read_probe = nuv_probe,
  290. .read_header = nuv_header,
  291. .read_packet = nuv_packet,
  292. .flags = AVFMT_GENERIC_INDEX,
  293. };