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.

242 lines
7.7KB

  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. } NUVContext;
  27. typedef enum {
  28. NUV_VIDEO = 'V',
  29. NUV_EXTRADATA = 'D',
  30. NUV_AUDIO = 'A',
  31. NUV_SEEKP = 'R',
  32. NUV_MYTHEXT = 'X'
  33. } frametype_t;
  34. static int nuv_probe(AVProbeData *p) {
  35. if (p->buf_size < 12)
  36. return 0;
  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. frametype_t 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. } else
  85. url_fskip(pb, 4);
  86. if (ast) {
  87. ast->codec->codec_tag = get_le32(pb);
  88. ast->codec->sample_rate = get_le32(pb);
  89. ast->codec->bits_per_sample = get_le32(pb);
  90. ast->codec->channels = get_le32(pb);
  91. ast->codec->codec_id =
  92. wav_codec_get_id(ast->codec->codec_tag,
  93. ast->codec->bits_per_sample);
  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 = (NUVContext *)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->codec_tag = MKTAG('R', 'J', 'P', 'G');
  142. vst->codec->width = width;
  143. vst->codec->height = height;
  144. vst->codec->bits_per_sample = 10;
  145. vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
  146. vst->r_frame_rate = av_d2q(fps, 60000);
  147. av_set_pts_info(vst, 32, 1, 1000);
  148. } else
  149. ctx->v_id = -1;
  150. if (a_packs) {
  151. ctx->a_id = stream_nr++;
  152. ast = av_new_stream(s, ctx->a_id);
  153. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  154. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  155. ast->codec->channels = 2;
  156. ast->codec->sample_rate = 44100;
  157. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  158. ast->codec->block_align = 2 * 2;
  159. ast->codec->bits_per_sample = 16;
  160. av_set_pts_info(ast, 32, 1, 1000);
  161. } else
  162. ctx->a_id = -1;
  163. get_codec_data(pb, vst, ast, is_mythtv);
  164. return 0;
  165. }
  166. #define HDRSIZE 12
  167. static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
  168. NUVContext *ctx = (NUVContext *)s->priv_data;
  169. ByteIOContext *pb = &s->pb;
  170. uint8_t hdr[HDRSIZE];
  171. frametype_t frametype;
  172. int ret, size;
  173. while (!url_feof(pb)) {
  174. ret = get_buffer(pb, hdr, HDRSIZE);
  175. if (ret <= 0)
  176. return ret ? ret : -1;
  177. frametype = hdr[0];
  178. size = PKTSIZE(AV_RL32(&hdr[8]));
  179. switch (frametype) {
  180. case NUV_VIDEO:
  181. case NUV_EXTRADATA:
  182. if (ctx->v_id < 0) {
  183. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  184. url_fskip(pb, size);
  185. break;
  186. }
  187. ret = av_new_packet(pkt, HDRSIZE + size);
  188. if (ret < 0)
  189. return ret;
  190. pkt->pos = url_ftell(pb);
  191. pkt->pts = AV_RL32(&hdr[4]);
  192. pkt->stream_index = ctx->v_id;
  193. memcpy(pkt->data, hdr, HDRSIZE);
  194. ret = get_buffer(pb, pkt->data + HDRSIZE, size);
  195. return ret;
  196. case NUV_AUDIO:
  197. if (ctx->a_id < 0) {
  198. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  199. url_fskip(pb, size);
  200. break;
  201. }
  202. ret = av_get_packet(pb, pkt, size);
  203. pkt->pts = AV_RL32(&hdr[4]);
  204. pkt->stream_index = ctx->a_id;
  205. return ret;
  206. case NUV_SEEKP:
  207. // contains no data, size value is invalid
  208. break;
  209. default:
  210. url_fskip(pb, size);
  211. break;
  212. }
  213. }
  214. return AVERROR_IO;
  215. }
  216. AVInputFormat nuv_demuxer = {
  217. "nuv",
  218. "NuppelVideo format",
  219. sizeof(NUVContext),
  220. nuv_probe,
  221. nuv_header,
  222. nuv_packet,
  223. NULL,
  224. NULL,
  225. };