libavformat/libnut.c
Go to the documentation of this file.
00001 /*
00002  * NUT (de)muxing via libnut
00003  * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "avformat.h"
00029 #include "internal.h"
00030 #include "riff.h"
00031 #include <libnut.h>
00032 
00033 #define ID_STRING "nut/multimedia container"
00034 #define ID_LENGTH (strlen(ID_STRING) + 1)
00035 
00036 typedef struct {
00037     nut_context_tt * nut;
00038     nut_stream_header_tt * s;
00039 } NUTContext;
00040 
00041 static const AVCodecTag nut_tags[] = {
00042     { CODEC_ID_MPEG4,  MKTAG('m', 'p', '4', 'v') },
00043     { CODEC_ID_MP3,    MKTAG('m', 'p', '3', ' ') },
00044     { CODEC_ID_VORBIS, MKTAG('v', 'r', 'b', 's') },
00045     { 0, 0 },
00046 };
00047 
00048 #if CONFIG_LIBNUT_MUXER
00049 static int av_write(void * h, size_t len, const uint8_t * buf) {
00050     AVIOContext * bc = h;
00051     avio_write(bc, buf, len);
00052     //avio_flush(bc);
00053     return len;
00054 }
00055 
00056 static int nut_write_header(AVFormatContext * avf) {
00057     NUTContext * priv = avf->priv_data;
00058     AVIOContext * bc = avf->pb;
00059     nut_muxer_opts_tt mopts = {
00060         .output = {
00061             .priv = bc,
00062             .write = av_write,
00063         },
00064         .alloc = { av_malloc, av_realloc, av_free },
00065         .write_index = 1,
00066         .realtime_stream = 0,
00067         .max_distance = 32768,
00068         .fti = NULL,
00069     };
00070     nut_stream_header_tt * s;
00071     int i;
00072 
00073     priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
00074 
00075     for (i = 0; i < avf->nb_streams; i++) {
00076         AVCodecContext * codec = avf->streams[i]->codec;
00077         int j;
00078         int fourcc = 0;
00079         int num, denom, ssize;
00080 
00081         s[i].type = codec->codec_type == AVMEDIA_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
00082 
00083         if (codec->codec_tag) fourcc = codec->codec_tag;
00084         else fourcc = ff_codec_get_tag(nut_tags, codec->codec_id);
00085 
00086         if (!fourcc) {
00087             if (codec->codec_type == AVMEDIA_TYPE_VIDEO) fourcc = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
00088             if (codec->codec_type == AVMEDIA_TYPE_AUDIO) fourcc = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
00089         }
00090 
00091         s[i].fourcc_len = 4;
00092         s[i].fourcc = av_malloc(s[i].fourcc_len);
00093         for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
00094 
00095         ff_parse_specific_params(codec, &num, &ssize, &denom);
00096         avpriv_set_pts_info(avf->streams[i], 60, denom, num);
00097 
00098         s[i].time_base.num = denom;
00099         s[i].time_base.den = num;
00100 
00101         s[i].fixed_fps = 0;
00102         s[i].decode_delay = codec->has_b_frames;
00103         s[i].codec_specific_len = codec->extradata_size;
00104         s[i].codec_specific = codec->extradata;
00105 
00106         if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00107             s[i].width = codec->width;
00108             s[i].height = codec->height;
00109             s[i].sample_width = 0;
00110             s[i].sample_height = 0;
00111             s[i].colorspace_type = 0;
00112         } else {
00113             s[i].samplerate_num = codec->sample_rate;
00114             s[i].samplerate_denom = 1;
00115             s[i].channel_count = codec->channels;
00116         }
00117     }
00118 
00119     s[avf->nb_streams].type = -1;
00120     priv->nut = nut_muxer_init(&mopts, s, NULL);
00121 
00122     return 0;
00123 }
00124 
00125 static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
00126     NUTContext * priv = avf->priv_data;
00127     nut_packet_tt p;
00128 
00129     p.len = pkt->size;
00130     p.stream = pkt->stream_index;
00131     p.pts = pkt->pts;
00132     p.flags = pkt->flags & AV_PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
00133     p.next_pts = 0;
00134 
00135     nut_write_frame_reorder(priv->nut, &p, pkt->data);
00136 
00137     return 0;
00138 }
00139 
00140 static int nut_write_trailer(AVFormatContext * avf) {
00141     AVIOContext * bc = avf->pb;
00142     NUTContext * priv = avf->priv_data;
00143     int i;
00144 
00145     nut_muxer_uninit_reorder(priv->nut);
00146     avio_flush(bc);
00147 
00148     for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
00149     av_freep(&priv->s);
00150 
00151     return 0;
00152 }
00153 
00154 AVOutputFormat ff_libnut_muxer = {
00155     .name              = "libnut",
00156     .long_name         = "nut format",
00157     .mime_type         = "video/x-nut",
00158     .extensions        = "nut",
00159     .priv_data_size    = sizeof(NUTContext),
00160     .audio_codec       = CODEC_ID_VORBIS,
00161     .video_codec       = CODEC_ID_MPEG4,
00162     .write_header      = nut_write_header,
00163     .write_packet      = nut_write_packet,
00164     .write_trailer     = nut_write_trailer,
00165     .flags = AVFMT_GLOBALHEADER,
00166 };
00167 #endif /* CONFIG_LIBNUT_MUXER */
00168 
00169 static int nut_probe(AVProbeData *p) {
00170     if (!memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
00171 
00172     return 0;
00173 }
00174 
00175 static size_t av_read(void * h, size_t len, uint8_t * buf) {
00176     AVIOContext * bc = h;
00177     return avio_read(bc, buf, len);
00178 }
00179 
00180 static off_t av_seek(void * h, long long pos, int whence) {
00181     AVIOContext * bc = h;
00182     if (whence == SEEK_END) {
00183         pos = avio_size(bc) + pos;
00184         whence = SEEK_SET;
00185     }
00186     return avio_seek(bc, pos, whence);
00187 }
00188 
00189 static int nut_read_header(AVFormatContext * avf, AVFormatParameters * ap) {
00190     NUTContext * priv = avf->priv_data;
00191     AVIOContext * bc = avf->pb;
00192     nut_demuxer_opts_tt dopts = {
00193         .input = {
00194             .priv = bc,
00195             .seek = av_seek,
00196             .read = av_read,
00197             .eof = NULL,
00198             .file_pos = 0,
00199         },
00200         .alloc = { av_malloc, av_realloc, av_free },
00201         .read_index = 1,
00202         .cache_syncpoints = 1,
00203     };
00204     nut_context_tt * nut = priv->nut = nut_demuxer_init(&dopts);
00205     nut_stream_header_tt * s;
00206     int ret, i;
00207 
00208     if ((ret = nut_read_headers(nut, &s, NULL))) {
00209         av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
00210         nut_demuxer_uninit(nut);
00211         return -1;
00212     }
00213 
00214     priv->s = s;
00215 
00216     for (i = 0; s[i].type != -1 && i < 2; i++) {
00217         AVStream * st = avformat_new_stream(avf, NULL);
00218         int j;
00219 
00220         for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
00221 
00222         st->codec->has_b_frames = s[i].decode_delay;
00223 
00224         st->codec->extradata_size = s[i].codec_specific_len;
00225         if (st->codec->extradata_size) {
00226             st->codec->extradata = av_mallocz(st->codec->extradata_size);
00227             memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
00228         }
00229 
00230         avpriv_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
00231         st->start_time = 0;
00232         st->duration = s[i].max_pts;
00233 
00234         st->codec->codec_id = ff_codec_get_id(nut_tags, st->codec->codec_tag);
00235 
00236         switch(s[i].type) {
00237         case NUT_AUDIO_CLASS:
00238             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00239             if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, st->codec->codec_tag);
00240 
00241             st->codec->channels = s[i].channel_count;
00242             st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
00243             break;
00244         case NUT_VIDEO_CLASS:
00245             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00246             if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, st->codec->codec_tag);
00247 
00248             st->codec->width = s[i].width;
00249             st->codec->height = s[i].height;
00250             st->sample_aspect_ratio.num = s[i].sample_width;
00251             st->sample_aspect_ratio.den = s[i].sample_height;
00252             break;
00253         }
00254         if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
00255     }
00256 
00257     return 0;
00258 }
00259 
00260 static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
00261     NUTContext * priv = avf->priv_data;
00262     nut_packet_tt pd;
00263     int ret;
00264 
00265     ret = nut_read_next_packet(priv->nut, &pd);
00266 
00267     if (ret || av_new_packet(pkt, pd.len) < 0) {
00268         if (ret != NUT_ERR_EOF)
00269             av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
00270         return -1;
00271     }
00272 
00273     if (pd.flags & NUT_FLAG_KEY) pkt->flags |= AV_PKT_FLAG_KEY;
00274     pkt->pts = pd.pts;
00275     pkt->stream_index = pd.stream;
00276     pkt->pos = avio_tell(avf->pb);
00277 
00278     ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
00279 
00280     return ret;
00281 }
00282 
00283 static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
00284     NUTContext * priv = avf->priv_data;
00285     int active_streams[] = { stream_index, -1 };
00286     double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
00287 
00288     if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
00289 
00290     return 0;
00291 }
00292 
00293 static int nut_read_close(AVFormatContext *s) {
00294     NUTContext * priv = s->priv_data;
00295 
00296     nut_demuxer_uninit(priv->nut);
00297 
00298     return 0;
00299 }
00300 
00301 AVInputFormat ff_libnut_demuxer = {
00302     .name           = "libnut",
00303     .long_name      = NULL_IF_CONFIG_SMALL("NUT format"),
00304     .priv_data_size = sizeof(NUTContext),
00305     .read_probe     = nut_probe,
00306     .read_header    = nut_read_header,
00307     .read_packet    = nut_read_packet,
00308     .read_close     = nut_read_close,
00309     .read_seek      = nut_read_seek,
00310     .extensions = "nut",
00311 };