00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "riff.h"
00023
00024 typedef struct {
00025 int v_id;
00026 int a_id;
00027 int rtjpg_video;
00028 } NUVContext;
00029
00030 typedef enum {
00031 NUV_VIDEO = 'V',
00032 NUV_EXTRADATA = 'D',
00033 NUV_AUDIO = 'A',
00034 NUV_SEEKP = 'R',
00035 NUV_MYTHEXT = 'X'
00036 } frametype_t;
00037
00038 static int nuv_probe(AVProbeData *p) {
00039 if (!memcmp(p->buf, "NuppelVideo", 12))
00040 return AVPROBE_SCORE_MAX;
00041 if (!memcmp(p->buf, "MythTVVideo", 12))
00042 return AVPROBE_SCORE_MAX;
00043 return 0;
00044 }
00045
00047 #define PKTSIZE(s) (s & 0xffffff)
00048
00056 static int get_codec_data(ByteIOContext *pb, AVStream *vst,
00057 AVStream *ast, int myth) {
00058 frametype_t frametype;
00059 if (!vst && !myth)
00060 return 1;
00061 while (!url_feof(pb)) {
00062 int size, subtype;
00063 frametype = get_byte(pb);
00064 switch (frametype) {
00065 case NUV_EXTRADATA:
00066 subtype = get_byte(pb);
00067 url_fskip(pb, 6);
00068 size = PKTSIZE(get_le32(pb));
00069 if (vst && subtype == 'R') {
00070 vst->codec->extradata_size = size;
00071 vst->codec->extradata = av_malloc(size);
00072 get_buffer(pb, vst->codec->extradata, size);
00073 size = 0;
00074 if (!myth)
00075 return 1;
00076 }
00077 break;
00078 case NUV_MYTHEXT:
00079 url_fskip(pb, 7);
00080 size = PKTSIZE(get_le32(pb));
00081 if (size != 128 * 4)
00082 break;
00083 get_le32(pb);
00084 if (vst) {
00085 vst->codec->codec_tag = get_le32(pb);
00086 vst->codec->codec_id =
00087 codec_get_id(codec_bmp_tags, vst->codec->codec_tag);
00088 if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
00089 vst->codec->codec_id = CODEC_ID_NUV;
00090 } else
00091 url_fskip(pb, 4);
00092
00093 if (ast) {
00094 ast->codec->codec_tag = get_le32(pb);
00095 ast->codec->sample_rate = get_le32(pb);
00096 ast->codec->bits_per_sample = get_le32(pb);
00097 ast->codec->channels = get_le32(pb);
00098 ast->codec->codec_id =
00099 wav_codec_get_id(ast->codec->codec_tag,
00100 ast->codec->bits_per_sample);
00101 ast->need_parsing = AVSTREAM_PARSE_FULL;
00102 } else
00103 url_fskip(pb, 4 * 4);
00104
00105 size -= 6 * 4;
00106 url_fskip(pb, size);
00107 return 1;
00108 case NUV_SEEKP:
00109 size = 11;
00110 break;
00111 default:
00112 url_fskip(pb, 7);
00113 size = PKTSIZE(get_le32(pb));
00114 break;
00115 }
00116 url_fskip(pb, size);
00117 }
00118 return 0;
00119 }
00120
00121 static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
00122 NUVContext *ctx = s->priv_data;
00123 ByteIOContext *pb = s->pb;
00124 char id_string[12], version_string[5];
00125 double aspect, fps;
00126 int is_mythtv, width, height, v_packs, a_packs;
00127 int stream_nr = 0;
00128 AVStream *vst = NULL, *ast = NULL;
00129 get_buffer(pb, id_string, 12);
00130 is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
00131 get_buffer(pb, version_string, 5);
00132 url_fskip(pb, 3);
00133 width = get_le32(pb);
00134 height = get_le32(pb);
00135 get_le32(pb);
00136 get_le32(pb);
00137 get_byte(pb);
00138 url_fskip(pb, 3);
00139 aspect = av_int2dbl(get_le64(pb));
00140 fps = av_int2dbl(get_le64(pb));
00141
00142
00143 v_packs = get_le32(pb);
00144 a_packs = get_le32(pb);
00145 get_le32(pb);
00146
00147 get_le32(pb);
00148
00149 if (v_packs) {
00150 ctx->v_id = stream_nr++;
00151 vst = av_new_stream(s, ctx->v_id);
00152 vst->codec->codec_type = CODEC_TYPE_VIDEO;
00153 vst->codec->codec_id = CODEC_ID_NUV;
00154 vst->codec->width = width;
00155 vst->codec->height = height;
00156 vst->codec->bits_per_sample = 10;
00157 vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
00158 vst->r_frame_rate = av_d2q(fps, 60000);
00159 av_set_pts_info(vst, 32, 1, 1000);
00160 } else
00161 ctx->v_id = -1;
00162
00163 if (a_packs) {
00164 ctx->a_id = stream_nr++;
00165 ast = av_new_stream(s, ctx->a_id);
00166 ast->codec->codec_type = CODEC_TYPE_AUDIO;
00167 ast->codec->codec_id = CODEC_ID_PCM_S16LE;
00168 ast->codec->channels = 2;
00169 ast->codec->sample_rate = 44100;
00170 ast->codec->bit_rate = 2 * 2 * 44100 * 8;
00171 ast->codec->block_align = 2 * 2;
00172 ast->codec->bits_per_sample = 16;
00173 av_set_pts_info(ast, 32, 1, 1000);
00174 } else
00175 ctx->a_id = -1;
00176
00177 get_codec_data(pb, vst, ast, is_mythtv);
00178 ctx->rtjpg_video = vst->codec->codec_id == CODEC_ID_NUV;
00179 return 0;
00180 }
00181
00182 #define HDRSIZE 12
00183
00184 static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
00185 NUVContext *ctx = s->priv_data;
00186 ByteIOContext *pb = s->pb;
00187 uint8_t hdr[HDRSIZE];
00188 frametype_t frametype;
00189 int ret, size;
00190 while (!url_feof(pb)) {
00191 int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
00192 ret = get_buffer(pb, hdr, HDRSIZE);
00193 if (ret <= 0)
00194 return ret ? ret : -1;
00195 frametype = hdr[0];
00196 size = PKTSIZE(AV_RL32(&hdr[8]));
00197 switch (frametype) {
00198 case NUV_EXTRADATA:
00199 if (!ctx->rtjpg_video) {
00200 url_fskip(pb, size);
00201 break;
00202 }
00203 case NUV_VIDEO:
00204 if (ctx->v_id < 0) {
00205 av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
00206 url_fskip(pb, size);
00207 break;
00208 }
00209 ret = av_new_packet(pkt, copyhdrsize + size);
00210 if (ret < 0)
00211 return ret;
00212 pkt->pos = url_ftell(pb) - copyhdrsize;
00213 pkt->pts = AV_RL32(&hdr[4]);
00214 pkt->stream_index = ctx->v_id;
00215 memcpy(pkt->data, hdr, copyhdrsize);
00216 ret = get_buffer(pb, pkt->data + copyhdrsize, size);
00217 return ret;
00218 case NUV_AUDIO:
00219 if (ctx->a_id < 0) {
00220 av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
00221 url_fskip(pb, size);
00222 break;
00223 }
00224 ret = av_get_packet(pb, pkt, size);
00225 pkt->pts = AV_RL32(&hdr[4]);
00226 pkt->stream_index = ctx->a_id;
00227 return ret;
00228 case NUV_SEEKP:
00229
00230 break;
00231 default:
00232 url_fskip(pb, size);
00233 break;
00234 }
00235 }
00236 return AVERROR(EIO);
00237 }
00238
00239 AVInputFormat nuv_demuxer = {
00240 "nuv",
00241 "NuppelVideo format",
00242 sizeof(NUVContext),
00243 nuv_probe,
00244 nuv_header,
00245 nuv_packet,
00246 NULL,
00247 NULL,
00248 };