5 #define ID_STRING "nut/multimedia container"
6 #define ID_LENGTH (strlen(ID_STRING) + 1)
10 nut_stream_header_t * s;
13 static const CodecTag nut_tags[] = {
14 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
15 { CODEC_ID_MP3, MKTAG('m', 'p', '3', ' ') },
16 { CODEC_ID_VORBIS, MKTAG('v', 'r', 'b', 's') },
21 static int av_write(void * h, size_t len, const uint8_t * buf) {
22 ByteIOContext * bc = h;
23 put_buffer(bc, buf, len);
24 //put_flush_packet(bc);
28 static int nut_write_header(AVFormatContext * avf) {
29 NUTContext * priv = avf->priv_data;
30 ByteIOContext * bc = &avf->pb;
31 nut_muxer_opts_t mopts = {
36 .alloc = { av_malloc, av_realloc, av_free },
39 .max_distance = 32768,
42 nut_stream_header_t * s;
45 priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
47 for (i = 0; i < avf->nb_streams; i++) {
48 AVCodecContext * codec = avf->streams[i]->codec;
51 int nom, denom, ssize;
53 s[i].type = codec->codec_type == CODEC_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
55 if (codec->codec_tag) fourcc = codec->codec_tag;
56 else fourcc = codec_get_tag(nut_tags, codec->codec_id);
59 if (codec->codec_type == CODEC_TYPE_VIDEO) fourcc = codec_get_bmp_tag(codec->codec_id);
60 if (codec->codec_type == CODEC_TYPE_AUDIO) fourcc = codec_get_wav_tag(codec->codec_id);
64 s[i].fourcc = av_malloc(s[i].fourcc_len);
65 for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
67 ff_parse_specific_params(codec, &nom, &ssize, &denom);
68 av_set_pts_info(avf->streams[i], 60, denom, nom);
70 s[i].time_base.nom = denom;
71 s[i].time_base.den = nom;
74 s[i].decode_delay = codec->has_b_frames;
75 s[i].codec_specific_len = codec->extradata_size;
76 s[i].codec_specific = codec->extradata;
78 if (codec->codec_type == CODEC_TYPE_VIDEO) {
79 s[i].width = codec->width;
80 s[i].height = codec->height;
81 s[i].sample_width = 0;
82 s[i].sample_height = 0;
83 s[i].colorspace_type = 0;
85 s[i].samplerate_nom = codec->sample_rate;
86 s[i].samplerate_denom = 1;
87 s[i].channel_count = codec->channels;
91 s[avf->nb_streams].type = -1;
92 priv->nut = nut_muxer_init(&mopts, s, NULL);
97 static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
98 NUTContext * priv = avf->priv_data;
102 p.stream = pkt->stream_index;
104 p.flags = pkt->flags & PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
107 nut_write_frame_reorder(priv->nut, &p, pkt->data);
112 static int nut_write_trailer(AVFormatContext * avf) {
113 ByteIOContext * bc = &avf->pb;
114 NUTContext * priv = avf->priv_data;
117 nut_muxer_uninit_reorder(priv->nut);
118 put_flush_packet(bc);
120 for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
126 AVOutputFormat nut_muxer = {
137 .flags = AVFMT_GLOBALHEADER,
139 #endif //CONFIG_MUXERS
141 static int nut_probe(AVProbeData *p) {
142 if (p->buf_size >= ID_LENGTH && !memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
147 static size_t av_read(void * h, size_t len, uint8_t * buf) {
148 ByteIOContext * bc = h;
149 return get_buffer(bc, buf, len);
152 static off_t av_seek(void * h, long long pos, int whence) {
153 ByteIOContext * bc = h;
154 if (whence == SEEK_END) {
155 pos = url_fsize(bc) + pos;
158 return url_fseek(bc, pos, whence);
161 static int nut_read_header(AVFormatContext * avf, AVFormatParameters * ap) {
162 NUTContext * priv = avf->priv_data;
163 ByteIOContext * bc = &avf->pb;
164 nut_demuxer_opts_t dopts = {
172 .alloc = { av_malloc, av_realloc, av_free },
174 .cache_syncpoints = 1,
176 nut_context_t * nut = priv->nut = nut_demuxer_init(&dopts);
177 nut_stream_header_t * s;
180 if ((ret = nut_read_headers(nut, &s, NULL))) {
181 av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
182 nut_demuxer_uninit(nut);
188 for (i = 0; s[i].type != -1 && i < 2; i++) {
189 AVStream * st = av_new_stream(avf, i);
192 for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
194 st->codec->has_b_frames = s[i].decode_delay;
196 st->codec->extradata_size = s[i].codec_specific_len;
197 if (st->codec->extradata_size) {
198 st->codec->extradata = av_mallocz(st->codec->extradata_size);
199 memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
202 av_set_pts_info(avf->streams[i], 60, s[i].time_base.nom, s[i].time_base.den);
204 st->duration = s[i].max_pts;
206 st->codec->codec_id = codec_get_id(nut_tags, st->codec->codec_tag);
209 case NUT_AUDIO_CLASS:
210 st->codec->codec_type = CODEC_TYPE_AUDIO;
211 if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = codec_get_wav_id(st->codec->codec_tag);
213 st->codec->channels = s[i].channel_count;
214 st->codec->sample_rate = s[i].samplerate_nom / s[i].samplerate_denom;
216 case NUT_VIDEO_CLASS:
217 st->codec->codec_type = CODEC_TYPE_VIDEO;
218 if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = codec_get_bmp_id(st->codec->codec_tag);
220 st->codec->width = s[i].width;
221 st->codec->height = s[i].height;
222 st->codec->sample_aspect_ratio.num = s[i].sample_width;
223 st->codec->sample_aspect_ratio.den = s[i].sample_height;
226 if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
232 static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
233 NUTContext * priv = avf->priv_data;
237 ret = nut_read_next_packet(priv->nut, &pd);
239 if (ret || av_new_packet(pkt, pd.len) < 0) {
240 if (ret != NUT_ERR_EOF)
241 av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
245 if (pd.flags & NUT_FLAG_KEY) pkt->flags |= PKT_FLAG_KEY;
247 pkt->stream_index = pd.stream;
248 pkt->pos = url_ftell(&avf->pb);
250 ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
255 static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
256 NUTContext * priv = avf->priv_data;
257 int active_streams[] = { stream_index, -1 };
258 double time_pos = target_ts * priv->s[stream_index].time_base.nom / (double)priv->s[stream_index].time_base.den;
260 if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
265 static int nut_read_close(AVFormatContext *s) {
266 NUTContext * priv = s->priv_data;
268 nut_demuxer_uninit(priv->nut);
273 AVInputFormat nut_demuxer = {