Statistics
| Branch: | Revision:

ffmpeg / libavformat / libnut.c @ 468f8102

History | View | Annotate | Download (8.02 KB)

1
#include "avformat.h"
2
#include "riff.h"
3
#include <libnut.h>
4

    
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
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') },
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
        .fti = NULL,
41
    };
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
        int nom, denom, ssize;
52

    
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
        ff_parse_specific_params(codec, &nom, &ssize, &denom);
68
        av_set_pts_info(avf->streams[i], 60, denom, nom);
69

    
70
        s[i].time_base.nom = denom;
71
        s[i].time_base.den = nom;
72

    
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
            s[i].samplerate_nom = codec->sample_rate;
86
            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
AVOutputFormat nut_muxer = {
127
    "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
        .read_index = 1,
174
        .cache_syncpoints = 1,
175
    };
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
        av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
182
        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
        av_set_pts_info(avf->streams[i], 60, s[i].time_base.nom, s[i].time_base.den);
203
        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
            st->codec->sample_rate = s[i].samplerate_nom / s[i].samplerate_denom;
215
            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
    ret = nut_read_next_packet(priv->nut, &pd);
238

    
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));
242
        return -1;
243
    }
244

    
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
    double time_pos = target_ts * priv->s[stream_index].time_base.nom / (double)priv->s[stream_index].time_base.den;
259

    
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
AVInputFormat nut_demuxer = {
274
    "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
};