nuv.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_readwrite.h"
  23. #include "avformat.h"
  24. #include "riff.h"
  25. typedef struct {
  26. int v_id;
  27. int a_id;
  28. int rtjpg_video;
  29. } NUVContext;
  30. typedef enum {
  31. NUV_VIDEO = 'V',
  32. NUV_EXTRADATA = 'D',
  33. NUV_AUDIO = 'A',
  34. NUV_SEEKP = 'R',
  35. NUV_MYTHEXT = 'X'
  36. } nuv_frametype;
  37. static int nuv_probe(AVProbeData *p) {
  38. if (!memcmp(p->buf, "NuppelVideo", 12))
  39. return AVPROBE_SCORE_MAX;
  40. if (!memcmp(p->buf, "MythTVVideo", 12))
  41. return AVPROBE_SCORE_MAX;
  42. return 0;
  43. }
  44. //! little macro to sanitize packet size
  45. #define PKTSIZE(s) (s & 0xffffff)
  46. /**
  47. * @brief read until we found all data needed for decoding
  48. * @param vst video stream of which to change parameters
  49. * @param ast video stream of which to change parameters
  50. * @param myth set if this is a MythTVVideo format file
  51. * @return 1 if all required codec data was found
  52. */
  53. static int get_codec_data(AVIOContext *pb, AVStream *vst,
  54. AVStream *ast, int myth) {
  55. nuv_frametype frametype;
  56. if (!vst && !myth)
  57. return 1; // no codec data needed
  58. while (!url_feof(pb)) {
  59. int size, subtype;
  60. frametype = avio_r8(pb);
  61. switch (frametype) {
  62. case NUV_EXTRADATA:
  63. subtype = avio_r8(pb);
  64. avio_skip(pb, 6);
  65. size = PKTSIZE(avio_rl32(pb));
  66. if (vst && subtype == 'R') {
  67. vst->codec->extradata_size = size;
  68. vst->codec->extradata = av_malloc(size);
  69. avio_read(pb, vst->codec->extradata, size);
  70. size = 0;
  71. if (!myth)
  72. return 1;
  73. }
  74. break;
  75. case NUV_MYTHEXT:
  76. avio_skip(pb, 7);
  77. size = PKTSIZE(avio_rl32(pb));
  78. if (size != 128 * 4)
  79. break;
  80. avio_rl32(pb); // version
  81. if (vst) {
  82. vst->codec->codec_tag = avio_rl32(pb);
  83. vst->codec->codec_id =
  84. ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
  85. if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
  86. vst->codec->codec_id = CODEC_ID_NUV;
  87. } else
  88. avio_skip(pb, 4);
  89. if (ast) {
  90. ast->codec->codec_tag = avio_rl32(pb);
  91. ast->codec->sample_rate = avio_rl32(pb);
  92. ast->codec->bits_per_coded_sample = avio_rl32(pb);
  93. ast->codec->channels = avio_rl32(pb);
  94. ast->codec->codec_id =
  95. ff_wav_codec_get_id(ast->codec->codec_tag,
  96. ast->codec->bits_per_coded_sample);
  97. ast->need_parsing = AVSTREAM_PARSE_FULL;
  98. } else
  99. avio_skip(pb, 4 * 4);
  100. size -= 6 * 4;
  101. avio_skip(pb, size);
  102. return 1;
  103. case NUV_SEEKP:
  104. size = 11;
  105. break;
  106. default:
  107. avio_skip(pb, 7);
  108. size = PKTSIZE(avio_rl32(pb));
  109. break;
  110. }
  111. avio_skip(pb, size);
  112. }
  113. return 0;
  114. }
  115. static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
  116. NUVContext *ctx = s->priv_data;
  117. AVIOContext *pb = s->pb;
  118. char id_string[12];
  119. double aspect, fps;
  120. int is_mythtv, width, height, v_packs, a_packs;
  121. int stream_nr = 0;
  122. AVStream *vst = NULL, *ast = NULL;
  123. avio_read(pb, id_string, 12);
  124. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  125. avio_skip(pb, 5); // version string
  126. avio_skip(pb, 3); // padding
  127. width = avio_rl32(pb);
  128. height = avio_rl32(pb);
  129. avio_rl32(pb); // unused, "desiredwidth"
  130. avio_rl32(pb); // unused, "desiredheight"
  131. avio_r8(pb); // 'P' == progressive, 'I' == interlaced
  132. avio_skip(pb, 3); // padding
  133. aspect = av_int2dbl(avio_rl64(pb));
  134. if (aspect > 0.9999 && aspect < 1.0001)
  135. aspect = 4.0 / 3.0;
  136. fps = av_int2dbl(avio_rl64(pb));
  137. // number of packets per stream type, -1 means unknown, e.g. streaming
  138. v_packs = avio_rl32(pb);
  139. a_packs = avio_rl32(pb);
  140. avio_rl32(pb); // text
  141. avio_rl32(pb); // keyframe distance (?)
  142. if (v_packs) {
  143. ctx->v_id = stream_nr++;
  144. vst = av_new_stream(s, ctx->v_id);
  145. if (!vst)
  146. return AVERROR(ENOMEM);
  147. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  148. vst->codec->codec_id = CODEC_ID_NUV;
  149. vst->codec->width = width;
  150. vst->codec->height = height;
  151. vst->codec->bits_per_coded_sample = 10;
  152. vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
  153. vst->r_frame_rate = av_d2q(fps, 60000);
  154. av_set_pts_info(vst, 32, 1, 1000);
  155. } else
  156. ctx->v_id = -1;
  157. if (a_packs) {
  158. ctx->a_id = stream_nr++;
  159. ast = av_new_stream(s, ctx->a_id);
  160. if (!ast)
  161. return AVERROR(ENOMEM);
  162. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  163. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  164. ast->codec->channels = 2;
  165. ast->codec->sample_rate = 44100;
  166. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  167. ast->codec->block_align = 2 * 2;
  168. ast->codec->bits_per_coded_sample = 16;
  169. av_set_pts_info(ast, 32, 1, 1000);
  170. } else
  171. ctx->a_id = -1;
  172. get_codec_data(pb, vst, ast, is_mythtv);
  173. ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
  174. return 0;
  175. }
  176. #define HDRSIZE 12
  177. static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
  178. NUVContext *ctx = s->priv_data;
  179. AVIOContext *pb = s->pb;
  180. uint8_t hdr[HDRSIZE];
  181. nuv_frametype frametype;
  182. int ret, size;
  183. while (!url_feof(pb)) {
  184. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  185. uint64_t pos = avio_tell(pb);
  186. ret = avio_read(pb, hdr, HDRSIZE);
  187. if (ret < HDRSIZE)
  188. return ret < 0 ? ret : AVERROR(EIO);
  189. frametype = hdr[0];
  190. size = PKTSIZE(AV_RL32(&hdr[8]));
  191. switch (frametype) {
  192. case NUV_EXTRADATA:
  193. if (!ctx->rtjpg_video) {
  194. avio_skip(pb, size);
  195. break;
  196. }
  197. case NUV_VIDEO:
  198. if (ctx->v_id < 0) {
  199. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  200. avio_skip(pb, size);
  201. break;
  202. }
  203. ret = av_new_packet(pkt, copyhdrsize + size);
  204. if (ret < 0)
  205. return ret;
  206. // HACK: we have no idea if it is a keyframe,
  207. // but if we mark none seeking will not work at all.
  208. pkt->flags |= AV_PKT_FLAG_KEY;
  209. pkt->pos = pos;
  210. pkt->pts = AV_RL32(&hdr[4]);
  211. pkt->stream_index = ctx->v_id;
  212. memcpy(pkt->data, hdr, copyhdrsize);
  213. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  214. if (ret < 0) {
  215. av_free_packet(pkt);
  216. return ret;
  217. }
  218. if (ret < size)
  219. av_shrink_packet(pkt, copyhdrsize + ret);
  220. return 0;
  221. case NUV_AUDIO:
  222. if (ctx->a_id < 0) {
  223. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  224. avio_skip(pb, size);
  225. break;
  226. }
  227. ret = av_get_packet(pb, pkt, size);
  228. pkt->flags |= AV_PKT_FLAG_KEY;
  229. pkt->pos = pos;
  230. pkt->pts = AV_RL32(&hdr[4]);
  231. pkt->stream_index = ctx->a_id;
  232. if (ret < 0) return ret;
  233. return 0;
  234. case NUV_SEEKP:
  235. // contains no data, size value is invalid
  236. break;
  237. default:
  238. avio_skip(pb, size);
  239. break;
  240. }
  241. }
  242. return AVERROR(EIO);
  243. }
  244. AVInputFormat ff_nuv_demuxer = {
  245. .name = "nuv",
  246. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo format"),
  247. .priv_data_size = sizeof(NUVContext),
  248. .read_probe = nuv_probe,
  249. .read_header = nuv_header,
  250. .read_packet = nuv_packet,
  251. .flags = AVFMT_GENERIC_INDEX,
  252. };