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.

248 lines
8.0KB

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