nuv.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(ByteIOContext *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 (!url_feof(pb)) {
  58. int size, subtype;
  59. frametype = get_byte(pb);
  60. switch (frametype) {
  61. case NUV_EXTRADATA:
  62. subtype = get_byte(pb);
  63. url_fskip(pb, 6);
  64. size = PKTSIZE(get_le32(pb));
  65. if (vst && subtype == 'R') {
  66. vst->codec->extradata_size = size;
  67. vst->codec->extradata = av_malloc(size);
  68. get_buffer(pb, vst->codec->extradata, size);
  69. size = 0;
  70. if (!myth)
  71. return 1;
  72. }
  73. break;
  74. case NUV_MYTHEXT:
  75. url_fskip(pb, 7);
  76. size = PKTSIZE(get_le32(pb));
  77. if (size != 128 * 4)
  78. break;
  79. get_le32(pb); // version
  80. if (vst) {
  81. vst->codec->codec_tag = get_le32(pb);
  82. vst->codec->codec_id =
  83. codec_get_id(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. url_fskip(pb, 4);
  88. if (ast) {
  89. ast->codec->codec_tag = get_le32(pb);
  90. ast->codec->sample_rate = get_le32(pb);
  91. ast->codec->bits_per_coded_sample = get_le32(pb);
  92. ast->codec->channels = get_le32(pb);
  93. ast->codec->codec_id =
  94. 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. url_fskip(pb, 4 * 4);
  99. size -= 6 * 4;
  100. url_fskip(pb, size);
  101. return 1;
  102. case NUV_SEEKP:
  103. size = 11;
  104. break;
  105. default:
  106. url_fskip(pb, 7);
  107. size = PKTSIZE(get_le32(pb));
  108. break;
  109. }
  110. url_fskip(pb, size);
  111. }
  112. return 0;
  113. }
  114. static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
  115. NUVContext *ctx = s->priv_data;
  116. ByteIOContext *pb = s->pb;
  117. char id_string[12], version_string[5];
  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. get_buffer(pb, id_string, 12);
  123. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  124. get_buffer(pb, version_string, 5);
  125. url_fskip(pb, 3); // padding
  126. width = get_le32(pb);
  127. height = get_le32(pb);
  128. get_le32(pb); // unused, "desiredwidth"
  129. get_le32(pb); // unused, "desiredheight"
  130. get_byte(pb); // 'P' == progressive, 'I' == interlaced
  131. url_fskip(pb, 3); // padding
  132. aspect = av_int2dbl(get_le64(pb));
  133. if (aspect > 0.9999 && aspect < 1.0001)
  134. aspect = 4.0 / 3.0;
  135. fps = av_int2dbl(get_le64(pb));
  136. // number of packets per stream type, -1 means unknown, e.g. streaming
  137. v_packs = get_le32(pb);
  138. a_packs = get_le32(pb);
  139. get_le32(pb); // text
  140. get_le32(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 = CODEC_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 = CODEC_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. ByteIOContext *pb = s->pb;
  179. uint8_t hdr[HDRSIZE];
  180. nuv_frametype frametype;
  181. int ret, size;
  182. while (!url_feof(pb)) {
  183. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  184. ret = get_buffer(pb, hdr, HDRSIZE);
  185. if (ret <= 0)
  186. return ret ? ret : -1;
  187. frametype = hdr[0];
  188. size = PKTSIZE(AV_RL32(&hdr[8]));
  189. switch (frametype) {
  190. case NUV_EXTRADATA:
  191. if (!ctx->rtjpg_video) {
  192. url_fskip(pb, size);
  193. break;
  194. }
  195. case NUV_VIDEO:
  196. if (ctx->v_id < 0) {
  197. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  198. url_fskip(pb, size);
  199. break;
  200. }
  201. ret = av_new_packet(pkt, copyhdrsize + size);
  202. if (ret < 0)
  203. return ret;
  204. pkt->pos = url_ftell(pb) - copyhdrsize;
  205. pkt->pts = AV_RL32(&hdr[4]);
  206. pkt->stream_index = ctx->v_id;
  207. memcpy(pkt->data, hdr, copyhdrsize);
  208. ret = get_buffer(pb, pkt->data + copyhdrsize, size);
  209. return ret;
  210. case NUV_AUDIO:
  211. if (ctx->a_id < 0) {
  212. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  213. url_fskip(pb, size);
  214. break;
  215. }
  216. ret = av_get_packet(pb, pkt, size);
  217. pkt->pts = AV_RL32(&hdr[4]);
  218. pkt->stream_index = ctx->a_id;
  219. return ret;
  220. case NUV_SEEKP:
  221. // contains no data, size value is invalid
  222. break;
  223. default:
  224. url_fskip(pb, size);
  225. break;
  226. }
  227. }
  228. return AVERROR(EIO);
  229. }
  230. AVInputFormat nuv_demuxer = {
  231. "nuv",
  232. NULL_IF_CONFIG_SMALL("NuppelVideo format"),
  233. sizeof(NUVContext),
  234. nuv_probe,
  235. nuv_header,
  236. nuv_packet,
  237. NULL,
  238. NULL,
  239. };