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.

271 lines
9.1KB

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