]> git.sesse.net Git - ffmpeg/blob - libavformat/libnut.c
quicktime 'raw ' support
[ffmpeg] / libavformat / libnut.c
1 /*
2  * NUT (de)muxing via libnut
3  * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
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
22 #include "avformat.h"
23 #include "riff.h"
24 #include <libnut.h>
25
26 #define ID_STRING "nut/multimedia container"
27 #define ID_LENGTH (strlen(ID_STRING) + 1)
28
29 typedef struct {
30     nut_context_t * nut;
31     nut_stream_header_t * s;
32 } NUTContext;
33
34 static const AVCodecTag nut_tags[] = {
35     { CODEC_ID_MPEG4,  MKTAG('m', 'p', '4', 'v') },
36     { CODEC_ID_MP3,    MKTAG('m', 'p', '3', ' ') },
37     { CODEC_ID_VORBIS, MKTAG('v', 'r', 'b', 's') },
38     { 0, 0 },
39 };
40
41 #ifdef CONFIG_MUXERS
42 static int av_write(void * h, size_t len, const uint8_t * buf) {
43     ByteIOContext * bc = h;
44     put_buffer(bc, buf, len);
45     //put_flush_packet(bc);
46     return len;
47 }
48
49 static int nut_write_header(AVFormatContext * avf) {
50     NUTContext * priv = avf->priv_data;
51     ByteIOContext * bc = &avf->pb;
52     nut_muxer_opts_t mopts = {
53         .output = {
54             .priv = bc,
55             .write = av_write,
56         },
57         .alloc = { av_malloc, av_realloc, av_free },
58         .write_index = 1,
59         .realtime_stream = 0,
60         .max_distance = 32768,
61         .fti = NULL,
62     };
63     nut_stream_header_t * s;
64     int i;
65
66     priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
67
68     for (i = 0; i < avf->nb_streams; i++) {
69         AVCodecContext * codec = avf->streams[i]->codec;
70         int j;
71         int fourcc = 0;
72         int num, denom, ssize;
73
74         s[i].type = codec->codec_type == CODEC_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
75
76         if (codec->codec_tag) fourcc = codec->codec_tag;
77         else fourcc = codec_get_tag(nut_tags, codec->codec_id);
78
79         if (!fourcc) {
80             if (codec->codec_type == CODEC_TYPE_VIDEO) fourcc = codec_get_bmp_tag(codec->codec_id);
81             if (codec->codec_type == CODEC_TYPE_AUDIO) fourcc = codec_get_wav_tag(codec->codec_id);
82         }
83
84         s[i].fourcc_len = 4;
85         s[i].fourcc = av_malloc(s[i].fourcc_len);
86         for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
87
88         ff_parse_specific_params(codec, &num, &ssize, &denom);
89         av_set_pts_info(avf->streams[i], 60, denom, num);
90
91         s[i].time_base.num = denom;
92         s[i].time_base.den = num;
93
94         s[i].fixed_fps = 0;
95         s[i].decode_delay = codec->has_b_frames;
96         s[i].codec_specific_len = codec->extradata_size;
97         s[i].codec_specific = codec->extradata;
98
99         if (codec->codec_type == CODEC_TYPE_VIDEO) {
100             s[i].width = codec->width;
101             s[i].height = codec->height;
102             s[i].sample_width = 0;
103             s[i].sample_height = 0;
104             s[i].colorspace_type = 0;
105         } else {
106             s[i].samplerate_num = codec->sample_rate;
107             s[i].samplerate_denom = 1;
108             s[i].channel_count = codec->channels;
109         }
110     }
111
112     s[avf->nb_streams].type = -1;
113     priv->nut = nut_muxer_init(&mopts, s, NULL);
114
115     return 0;
116 }
117
118 static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
119     NUTContext * priv = avf->priv_data;
120     nut_packet_t p;
121
122     p.len = pkt->size;
123     p.stream = pkt->stream_index;
124     p.pts = pkt->pts;
125     p.flags = pkt->flags & PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
126     p.next_pts = 0;
127
128     nut_write_frame_reorder(priv->nut, &p, pkt->data);
129
130     return 0;
131 }
132
133 static int nut_write_trailer(AVFormatContext * avf) {
134     ByteIOContext * bc = &avf->pb;
135     NUTContext * priv = avf->priv_data;
136     int i;
137
138     nut_muxer_uninit_reorder(priv->nut);
139     put_flush_packet(bc);
140
141     for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
142     av_freep(&priv->s);
143
144     return 0;
145 }
146
147 AVOutputFormat libnut_muxer = {
148     "nut",
149     "nut format",
150     "video/x-nut",
151     "nut",
152     sizeof(NUTContext),
153     CODEC_ID_VORBIS,
154     CODEC_ID_MPEG4,
155     nut_write_header,
156     nut_write_packet,
157     nut_write_trailer,
158     .flags = AVFMT_GLOBALHEADER,
159 };
160 #endif //CONFIG_MUXERS
161
162 static int nut_probe(AVProbeData *p) {
163     if (p->buf_size >= ID_LENGTH && !memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
164
165     return 0;
166 }
167
168 static size_t av_read(void * h, size_t len, uint8_t * buf) {
169     ByteIOContext * bc = h;
170     return get_buffer(bc, buf, len);
171 }
172
173 static off_t av_seek(void * h, long long pos, int whence) {
174     ByteIOContext * bc = h;
175     if (whence == SEEK_END) {
176         pos = url_fsize(bc) + pos;
177         whence = SEEK_SET;
178     }
179     return url_fseek(bc, pos, whence);
180 }
181
182 static int nut_read_header(AVFormatContext * avf, AVFormatParameters * ap) {
183     NUTContext * priv = avf->priv_data;
184     ByteIOContext * bc = &avf->pb;
185     nut_demuxer_opts_t dopts = {
186         .input = {
187             .priv = bc,
188             .seek = av_seek,
189             .read = av_read,
190             .eof = NULL,
191             .file_pos = 0,
192         },
193         .alloc = { av_malloc, av_realloc, av_free },
194         .read_index = 1,
195         .cache_syncpoints = 1,
196     };
197     nut_context_t * nut = priv->nut = nut_demuxer_init(&dopts);
198     nut_stream_header_t * s;
199     int ret, i;
200
201     if ((ret = nut_read_headers(nut, &s, NULL))) {
202         av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
203         nut_demuxer_uninit(nut);
204         return -1;
205     }
206
207     priv->s = s;
208
209     for (i = 0; s[i].type != -1 && i < 2; i++) {
210         AVStream * st = av_new_stream(avf, i);
211         int j;
212
213         for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
214
215         st->codec->has_b_frames = s[i].decode_delay;
216
217         st->codec->extradata_size = s[i].codec_specific_len;
218         if (st->codec->extradata_size) {
219             st->codec->extradata = av_mallocz(st->codec->extradata_size);
220             memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
221         }
222
223         av_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
224         st->start_time = 0;
225         st->duration = s[i].max_pts;
226
227         st->codec->codec_id = codec_get_id(nut_tags, st->codec->codec_tag);
228
229         switch(s[i].type) {
230         case NUT_AUDIO_CLASS:
231             st->codec->codec_type = CODEC_TYPE_AUDIO;
232             if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = codec_get_wav_id(st->codec->codec_tag);
233
234             st->codec->channels = s[i].channel_count;
235             st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
236             break;
237         case NUT_VIDEO_CLASS:
238             st->codec->codec_type = CODEC_TYPE_VIDEO;
239             if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = codec_get_bmp_id(st->codec->codec_tag);
240
241             st->codec->width = s[i].width;
242             st->codec->height = s[i].height;
243             st->codec->sample_aspect_ratio.num = s[i].sample_width;
244             st->codec->sample_aspect_ratio.den = s[i].sample_height;
245             break;
246         }
247         if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
248     }
249
250     return 0;
251 }
252
253 static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
254     NUTContext * priv = avf->priv_data;
255     nut_packet_t pd;
256     int ret;
257
258     ret = nut_read_next_packet(priv->nut, &pd);
259
260     if (ret || av_new_packet(pkt, pd.len) < 0) {
261         if (ret != NUT_ERR_EOF)
262             av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
263         return -1;
264     }
265
266     if (pd.flags & NUT_FLAG_KEY) pkt->flags |= PKT_FLAG_KEY;
267     pkt->pts = pd.pts;
268     pkt->stream_index = pd.stream;
269     pkt->pos = url_ftell(&avf->pb);
270
271     ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
272
273     return ret;
274 }
275
276 static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
277     NUTContext * priv = avf->priv_data;
278     int active_streams[] = { stream_index, -1 };
279     double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
280
281     if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
282
283     return 0;
284 }
285
286 static int nut_read_close(AVFormatContext *s) {
287     NUTContext * priv = s->priv_data;
288
289     nut_demuxer_uninit(priv->nut);
290
291     return 0;
292 }
293
294 AVInputFormat libnut_demuxer = {
295     "nut",
296     "nut format",
297     sizeof(NUTContext),
298     nut_probe,
299     nut_read_header,
300     nut_read_packet,
301     nut_read_close,
302     nut_read_seek,
303     .extensions = "nut",
304 };