Statistics
| Branch: | Revision:

ffmpeg / libavformat / libnut.c @ 80a289b9

History | View | Annotate | Download (8.03 KB)

1 f956e129 Oded Shimon
#include "avformat.h"
2
#include "riff.h"
3 4d8aa1c3 Oded Shimon
#include <libnut.h>
4 f956e129 Oded Shimon
5
#define ID_STRING "nut/multimedia container"
6
#define ID_LENGTH (strlen(ID_STRING) + 1)
7
8
typedef struct {
9
    nut_context_t * nut;
10
    nut_stream_header_t * s;
11
} NUTContext;
12
13 7caf0cc6 Michael Niedermayer
static const AVCodecTag nut_tags[] = {
14 468f8102 Oded Shimon
    { CODEC_ID_MPEG4,  MKTAG('m', 'p', '4', 'v') },
15 f956e129 Oded Shimon
    { CODEC_ID_MP3,    MKTAG('m', 'p', '3', ' ') },
16
    { CODEC_ID_VORBIS, MKTAG('v', 'r', 'b', 's') },
17
    { 0, 0 },
18
};
19
20
#ifdef CONFIG_MUXERS
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);
25
    return len;
26
}
27
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 = {
32
        .output = {
33
            .priv = bc,
34
            .write = av_write,
35
        },
36
        .alloc = { av_malloc, av_realloc, av_free },
37
        .write_index = 1,
38
        .realtime_stream = 0,
39
        .max_distance = 32768,
40 bf054f74 Oded Shimon
        .fti = NULL,
41 f956e129 Oded Shimon
    };
42
    nut_stream_header_t * s;
43
    int i;
44
45
    priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
46
47
    for (i = 0; i < avf->nb_streams; i++) {
48
        AVCodecContext * codec = avf->streams[i]->codec;
49
        int j;
50
        int fourcc = 0;
51 d7d3efae Oded Shimon
        int num, denom, ssize;
52 f956e129 Oded Shimon
53
        s[i].type = codec->codec_type == CODEC_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
54
55
        if (codec->codec_tag) fourcc = codec->codec_tag;
56
        else fourcc = codec_get_tag(nut_tags, codec->codec_id);
57
58
        if (!fourcc) {
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);
61
        }
62
63
        s[i].fourcc_len = 4;
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;
66
67 d7d3efae Oded Shimon
        ff_parse_specific_params(codec, &num, &ssize, &denom);
68
        av_set_pts_info(avf->streams[i], 60, denom, num);
69 f956e129 Oded Shimon
70 d7d3efae Oded Shimon
        s[i].time_base.num = denom;
71
        s[i].time_base.den = num;
72 f956e129 Oded Shimon
73
        s[i].fixed_fps = 0;
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;
77
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;
84
        } else {
85 d7d3efae Oded Shimon
            s[i].samplerate_num = codec->sample_rate;
86 f956e129 Oded Shimon
            s[i].samplerate_denom = 1;
87
            s[i].channel_count = codec->channels;
88
        }
89
    }
90
91
    s[avf->nb_streams].type = -1;
92
    priv->nut = nut_muxer_init(&mopts, s, NULL);
93
94
    return 0;
95
}
96
97
static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
98
    NUTContext * priv = avf->priv_data;
99
    nut_packet_t p;
100
101
    p.len = pkt->size;
102
    p.stream = pkt->stream_index;
103
    p.pts = pkt->pts;
104
    p.flags = pkt->flags & PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
105
    p.next_pts = 0;
106
107
    nut_write_frame_reorder(priv->nut, &p, pkt->data);
108
109
    return 0;
110
}
111
112
static int nut_write_trailer(AVFormatContext * avf) {
113
    ByteIOContext * bc = &avf->pb;
114
    NUTContext * priv = avf->priv_data;
115
    int i;
116
117
    nut_muxer_uninit_reorder(priv->nut);
118
    put_flush_packet(bc);
119
120
    for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
121
    av_freep(&priv->s);
122
123
    return 0;
124
}
125
126 d7c59247 Diego Biurrun
AVOutputFormat libnut_muxer = {
127 f956e129 Oded Shimon
    "nut",
128
    "nut format",
129
    "video/x-nut",
130
    "nut",
131
    sizeof(NUTContext),
132
    CODEC_ID_VORBIS,
133
    CODEC_ID_MPEG4,
134
    nut_write_header,
135
    nut_write_packet,
136
    nut_write_trailer,
137
    .flags = AVFMT_GLOBALHEADER,
138
};
139
#endif //CONFIG_MUXERS
140
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;
143
144
    return 0;
145
}
146
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);
150
}
151
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;
156
        whence = SEEK_SET;
157
    }
158
    return url_fseek(bc, pos, whence);
159
}
160
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 = {
165
        .input = {
166
            .priv = bc,
167
            .seek = av_seek,
168
            .read = av_read,
169
            .eof = NULL,
170
            .file_pos = 0,
171
        },
172
        .alloc = { av_malloc, av_realloc, av_free },
173 0d666a47 Oded Shimon
        .read_index = 1,
174
        .cache_syncpoints = 1,
175 f956e129 Oded Shimon
    };
176
    nut_context_t * nut = priv->nut = nut_demuxer_init(&dopts);
177
    nut_stream_header_t * s;
178
    int ret, i;
179
180
    if ((ret = nut_read_headers(nut, &s, NULL))) {
181 e4bb7083 Oded Shimon
        av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
182 f956e129 Oded Shimon
        nut_demuxer_uninit(nut);
183
        return -1;
184
    }
185
186
    priv->s = s;
187
188
    for (i = 0; s[i].type != -1 && i < 2; i++) {
189
        AVStream * st = av_new_stream(avf, i);
190
        int j;
191
192
        for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
193
194
        st->codec->has_b_frames = s[i].decode_delay;
195
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);
200
        }
201
202 d7d3efae Oded Shimon
        av_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
203 f956e129 Oded Shimon
        st->start_time = 0;
204
        st->duration = s[i].max_pts;
205
206
        st->codec->codec_id = codec_get_id(nut_tags, st->codec->codec_tag);
207
208
        switch(s[i].type) {
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);
212
213
            st->codec->channels = s[i].channel_count;
214 d7d3efae Oded Shimon
            st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
215 f956e129 Oded Shimon
            break;
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);
219
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;
224
            break;
225
        }
226
        if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
227
    }
228
229
    return 0;
230
}
231
232
static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
233
    NUTContext * priv = avf->priv_data;
234
    nut_packet_t pd;
235
    int ret;
236
237 e4bb7083 Oded Shimon
    ret = nut_read_next_packet(priv->nut, &pd);
238 f956e129 Oded Shimon
239 e4bb7083 Oded Shimon
    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));
242
        return -1;
243
    }
244 f956e129 Oded Shimon
245
    if (pd.flags & NUT_FLAG_KEY) pkt->flags |= PKT_FLAG_KEY;
246
    pkt->pts = pd.pts;
247
    pkt->stream_index = pd.stream;
248
    pkt->pos = url_ftell(&avf->pb);
249
250
    ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
251
252
    return ret;
253
}
254
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 d7d3efae Oded Shimon
    double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
259 f956e129 Oded Shimon
260
    if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
261
262
    return 0;
263
}
264
265
static int nut_read_close(AVFormatContext *s) {
266
    NUTContext * priv = s->priv_data;
267
268
    nut_demuxer_uninit(priv->nut);
269
270
    return 0;
271
}
272
273 d7c59247 Diego Biurrun
AVInputFormat libnut_demuxer = {
274 f956e129 Oded Shimon
    "nut",
275
    "nut format",
276
    sizeof(NUTContext),
277
    nut_probe,
278
    nut_read_header,
279
    nut_read_packet,
280
    nut_read_close,
281
    nut_read_seek,
282
    .extensions = "nut",
283
};