00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/intfloat.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "riff.h"
00027
00028 typedef struct {
00029 int v_id;
00030 int a_id;
00031 int rtjpg_video;
00032 } NUVContext;
00033
00034 typedef enum {
00035 NUV_VIDEO = 'V',
00036 NUV_EXTRADATA = 'D',
00037 NUV_AUDIO = 'A',
00038 NUV_SEEKP = 'R',
00039 NUV_MYTHEXT = 'X'
00040 } nuv_frametype;
00041
00042 static int nuv_probe(AVProbeData *p) {
00043 if (!memcmp(p->buf, "NuppelVideo", 12))
00044 return AVPROBE_SCORE_MAX;
00045 if (!memcmp(p->buf, "MythTVVideo", 12))
00046 return AVPROBE_SCORE_MAX;
00047 return 0;
00048 }
00049
00051 #define PKTSIZE(s) (s & 0xffffff)
00052
00060 static int get_codec_data(AVIOContext *pb, AVStream *vst,
00061 AVStream *ast, int myth) {
00062 nuv_frametype frametype;
00063 if (!vst && !myth)
00064 return 1;
00065 while (!url_feof(pb)) {
00066 int size, subtype;
00067 frametype = avio_r8(pb);
00068 switch (frametype) {
00069 case NUV_EXTRADATA:
00070 subtype = avio_r8(pb);
00071 avio_skip(pb, 6);
00072 size = PKTSIZE(avio_rl32(pb));
00073 if (vst && subtype == 'R') {
00074 vst->codec->extradata_size = size;
00075 vst->codec->extradata = av_malloc(size);
00076 avio_read(pb, vst->codec->extradata, size);
00077 size = 0;
00078 if (!myth)
00079 return 1;
00080 }
00081 break;
00082 case NUV_MYTHEXT:
00083 avio_skip(pb, 7);
00084 size = PKTSIZE(avio_rl32(pb));
00085 if (size != 128 * 4)
00086 break;
00087 avio_rl32(pb);
00088 if (vst) {
00089 vst->codec->codec_tag = avio_rl32(pb);
00090 vst->codec->codec_id =
00091 ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
00092 if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
00093 vst->codec->codec_id = CODEC_ID_NUV;
00094 } else
00095 avio_skip(pb, 4);
00096
00097 if (ast) {
00098 ast->codec->codec_tag = avio_rl32(pb);
00099 ast->codec->sample_rate = avio_rl32(pb);
00100 ast->codec->bits_per_coded_sample = avio_rl32(pb);
00101 ast->codec->channels = avio_rl32(pb);
00102 ast->codec->codec_id =
00103 ff_wav_codec_get_id(ast->codec->codec_tag,
00104 ast->codec->bits_per_coded_sample);
00105 ast->need_parsing = AVSTREAM_PARSE_FULL;
00106 } else
00107 avio_skip(pb, 4 * 4);
00108
00109 size -= 6 * 4;
00110 avio_skip(pb, size);
00111 return 1;
00112 case NUV_SEEKP:
00113 size = 11;
00114 break;
00115 default:
00116 avio_skip(pb, 7);
00117 size = PKTSIZE(avio_rl32(pb));
00118 break;
00119 }
00120 avio_skip(pb, size);
00121 }
00122 return 0;
00123 }
00124
00125 static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
00126 NUVContext *ctx = s->priv_data;
00127 AVIOContext *pb = s->pb;
00128 char id_string[12];
00129 double aspect, fps;
00130 int is_mythtv, width, height, v_packs, a_packs;
00131 int stream_nr = 0;
00132 AVStream *vst = NULL, *ast = NULL;
00133 avio_read(pb, id_string, 12);
00134 is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
00135 avio_skip(pb, 5);
00136 avio_skip(pb, 3);
00137 width = avio_rl32(pb);
00138 height = avio_rl32(pb);
00139 avio_rl32(pb);
00140 avio_rl32(pb);
00141 avio_r8(pb);
00142 avio_skip(pb, 3);
00143 aspect = av_int2double(avio_rl64(pb));
00144 if (aspect > 0.9999 && aspect < 1.0001)
00145 aspect = 4.0 / 3.0;
00146 fps = av_int2double(avio_rl64(pb));
00147
00148
00149 v_packs = avio_rl32(pb);
00150 a_packs = avio_rl32(pb);
00151 avio_rl32(pb);
00152
00153 avio_rl32(pb);
00154
00155 if (v_packs) {
00156 ctx->v_id = stream_nr++;
00157 vst = avformat_new_stream(s, NULL);
00158 if (!vst)
00159 return AVERROR(ENOMEM);
00160 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00161 vst->codec->codec_id = CODEC_ID_NUV;
00162 vst->codec->width = width;
00163 vst->codec->height = height;
00164 vst->codec->bits_per_coded_sample = 10;
00165 vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
00166 vst->r_frame_rate = av_d2q(fps, 60000);
00167 avpriv_set_pts_info(vst, 32, 1, 1000);
00168 } else
00169 ctx->v_id = -1;
00170
00171 if (a_packs) {
00172 ctx->a_id = stream_nr++;
00173 ast = avformat_new_stream(s, NULL);
00174 if (!ast)
00175 return AVERROR(ENOMEM);
00176 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00177 ast->codec->codec_id = CODEC_ID_PCM_S16LE;
00178 ast->codec->channels = 2;
00179 ast->codec->sample_rate = 44100;
00180 ast->codec->bit_rate = 2 * 2 * 44100 * 8;
00181 ast->codec->block_align = 2 * 2;
00182 ast->codec->bits_per_coded_sample = 16;
00183 avpriv_set_pts_info(ast, 32, 1, 1000);
00184 } else
00185 ctx->a_id = -1;
00186
00187 get_codec_data(pb, vst, ast, is_mythtv);
00188 ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
00189 return 0;
00190 }
00191
00192 #define HDRSIZE 12
00193
00194 static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
00195 NUVContext *ctx = s->priv_data;
00196 AVIOContext *pb = s->pb;
00197 uint8_t hdr[HDRSIZE];
00198 nuv_frametype frametype;
00199 int ret, size;
00200 while (!url_feof(pb)) {
00201 int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
00202 uint64_t pos = avio_tell(pb);
00203 ret = avio_read(pb, hdr, HDRSIZE);
00204 if (ret < HDRSIZE)
00205 return ret < 0 ? ret : AVERROR(EIO);
00206 frametype = hdr[0];
00207 size = PKTSIZE(AV_RL32(&hdr[8]));
00208 switch (frametype) {
00209 case NUV_EXTRADATA:
00210 if (!ctx->rtjpg_video) {
00211 avio_skip(pb, size);
00212 break;
00213 }
00214 case NUV_VIDEO:
00215 if (ctx->v_id < 0) {
00216 av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
00217 avio_skip(pb, size);
00218 break;
00219 }
00220 ret = av_new_packet(pkt, copyhdrsize + size);
00221 if (ret < 0)
00222 return ret;
00223
00224 pkt->pos = pos;
00225 pkt->flags |= hdr[2] == 0 ? AV_PKT_FLAG_KEY : 0;
00226 pkt->pts = AV_RL32(&hdr[4]);
00227 pkt->stream_index = ctx->v_id;
00228 memcpy(pkt->data, hdr, copyhdrsize);
00229 ret = avio_read(pb, pkt->data + copyhdrsize, size);
00230 if (ret < 0) {
00231 av_free_packet(pkt);
00232 return ret;
00233 }
00234 if (ret < size)
00235 av_shrink_packet(pkt, copyhdrsize + ret);
00236 return 0;
00237 case NUV_AUDIO:
00238 if (ctx->a_id < 0) {
00239 av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
00240 avio_skip(pb, size);
00241 break;
00242 }
00243 ret = av_get_packet(pb, pkt, size);
00244 pkt->flags |= AV_PKT_FLAG_KEY;
00245 pkt->pos = pos;
00246 pkt->pts = AV_RL32(&hdr[4]);
00247 pkt->stream_index = ctx->a_id;
00248 if (ret < 0) return ret;
00249 return 0;
00250 case NUV_SEEKP:
00251
00252 break;
00253 default:
00254 avio_skip(pb, size);
00255 break;
00256 }
00257 }
00258 return AVERROR(EIO);
00259 }
00260
00265 static int nuv_resync(AVFormatContext *s, int64_t pos_limit) {
00266 AVIOContext *pb = s->pb;
00267 uint32_t tag = 0;
00268 while(!url_feof(pb) && avio_tell(pb) < pos_limit) {
00269 tag = (tag << 8) | avio_r8(pb);
00270 if (tag == MKBETAG('R','T','j','j') &&
00271 (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j') &&
00272 (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j'))
00273 return 1;
00274 }
00275 return 0;
00276 }
00277
00282 static int64_t nuv_read_dts(AVFormatContext *s, int stream_index,
00283 int64_t *ppos, int64_t pos_limit)
00284 {
00285 NUVContext *ctx = s->priv_data;
00286 AVIOContext *pb = s->pb;
00287 uint8_t hdr[HDRSIZE];
00288 nuv_frametype frametype;
00289 int size, key, idx;
00290 int64_t pos, dts;
00291
00292 if (avio_seek(pb, *ppos, SEEK_SET) < 0)
00293 return AV_NOPTS_VALUE;
00294
00295 if (!nuv_resync(s, pos_limit))
00296 return AV_NOPTS_VALUE;
00297
00298 while (!url_feof(pb) && avio_tell(pb) < pos_limit) {
00299 if (avio_read(pb, hdr, HDRSIZE) < HDRSIZE)
00300 return AV_NOPTS_VALUE;
00301 frametype = hdr[0];
00302 size = PKTSIZE(AV_RL32(&hdr[8]));
00303 switch (frametype) {
00304 case NUV_SEEKP:
00305 break;
00306 case NUV_AUDIO:
00307 case NUV_VIDEO:
00308 if (frametype == NUV_VIDEO) {
00309 idx = ctx->v_id;
00310 key = hdr[2] == 0;
00311 } else {
00312 idx = ctx->a_id;
00313 key = 1;
00314 }
00315 if (stream_index == idx) {
00316
00317 pos = avio_tell(s->pb) - HDRSIZE;
00318 dts = AV_RL32(&hdr[4]);
00319
00320
00321 av_add_index_entry(s->streams[stream_index], pos, dts, size + HDRSIZE, 0,
00322 key ? AVINDEX_KEYFRAME : 0);
00323
00324 *ppos = pos;
00325 return dts;
00326 }
00327 default:
00328 avio_skip(pb, size);
00329 break;
00330 }
00331 }
00332 return AV_NOPTS_VALUE;
00333 }
00334
00335
00336 AVInputFormat ff_nuv_demuxer = {
00337 .name = "nuv",
00338 .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo format"),
00339 .priv_data_size = sizeof(NUVContext),
00340 .read_probe = nuv_probe,
00341 .read_header = nuv_header,
00342 .read_packet = nuv_packet,
00343 .read_timestamp = nuv_read_dts,
00344 .flags = AVFMT_GENERIC_INDEX,
00345 };