Browse code

libavformat: remove the ffmenc and ffmdec muxer and demuxers

Used only by ffserver.

Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>

Rostislav Pehlivanov authored on 2017/10/22 03:38:59
Showing 6 changed files
... ...
@@ -36,6 +36,7 @@ version <next>:
36 36
 - aiir audio filter
37 37
 - aiff: add support for CD-ROM XA ADPCM
38 38
 - Removed the ffserver program
39
+- Removed the ffmenc and ffmdec muxer and demuxer
39 40
 
40 41
 
41 42
 version 3.4:
... ...
@@ -161,8 +161,6 @@ OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
161 161
 OBJS-$(CONFIG_EAC3_DEMUXER)              += ac3dec.o rawdec.o
162 162
 OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
163 163
 OBJS-$(CONFIG_EPAF_DEMUXER)              += epafdec.o pcm.o
164
-OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
165
-OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
166 164
 OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += ffmetadec.o
167 165
 OBJS-$(CONFIG_FFMETADATA_MUXER)          += ffmetaenc.o
168 166
 OBJS-$(CONFIG_FIFO_MUXER)                += fifo.o
... ...
@@ -120,7 +120,6 @@ static void register_all(void)
120 120
     REGISTER_MUXDEMUX(EAC3,             eac3);
121 121
     REGISTER_DEMUXER (EPAF,             epaf);
122 122
     REGISTER_MUXER   (F4V,              f4v);
123
-    REGISTER_MUXDEMUX(FFM,              ffm);
124 123
     REGISTER_MUXDEMUX(FFMETADATA,       ffmetadata);
125 124
     REGISTER_MUXER   (FIFO,             fifo);
126 125
     REGISTER_MUXDEMUX(FILMSTRIP,        filmstrip);
127 126
deleted file mode 100644
... ...
@@ -1,62 +0,0 @@
1
-/*
2
- * FFM (ffserver live feed) common header
3
- * Copyright (c) 2001 Fabrice Bellard
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
-#ifndef AVFORMAT_FFM_H
23
-#define AVFORMAT_FFM_H
24
-
25
-#include <stdint.h>
26
-#include "avformat.h"
27
-#include "avio.h"
28
-
29
-/* The FFM file is made of blocks of fixed size */
30
-#define FFM_HEADER_SIZE 14
31
-#define FFM_PACKET_SIZE 4096
32
-#define PACKET_ID       0x666d
33
-
34
-/* each packet contains frames (which can span several packets */
35
-#define FRAME_HEADER_SIZE    16
36
-#define FLAG_KEY_FRAME       0x01
37
-#define FLAG_DTS             0x02
38
-
39
-enum {
40
-    READ_HEADER,
41
-    READ_DATA,
42
-};
43
-
44
-typedef struct FFMContext {
45
-    const AVClass *class;
46
-    /* only reading mode */
47
-    int64_t write_index, file_size;
48
-    int read_state;
49
-    uint8_t header[FRAME_HEADER_SIZE+4];
50
-
51
-    /* read and write */
52
-    int first_packet; /* true if first packet, needed to set the discontinuity tag */
53
-    int packet_size;
54
-    int frame_offset;
55
-    int64_t dts;
56
-    uint8_t *packet_ptr, *packet_end;
57
-    uint8_t packet[FFM_PACKET_SIZE];
58
-    int64_t start_time;
59
-    int server_attached;
60
-} FFMContext;
61
-
62
-#endif /* AVFORMAT_FFM_H */
63 1
deleted file mode 100644
... ...
@@ -1,878 +0,0 @@
1
-/*
2
- * FFM (ffserver live feed) demuxer
3
- * Copyright (c) 2001 Fabrice Bellard
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 <stdint.h>
23
-
24
-#include "libavutil/imgutils.h"
25
-#include "libavutil/internal.h"
26
-#include "libavutil/intreadwrite.h"
27
-#include "libavutil/intfloat.h"
28
-#include "libavutil/opt.h"
29
-#include "libavutil/avassert.h"
30
-#include "libavutil/avstring.h"
31
-#include "libavutil/pixdesc.h"
32
-#include "libavcodec/internal.h"
33
-#include "avformat.h"
34
-#include "internal.h"
35
-#include "ffm.h"
36
-#include "avio_internal.h"
37
-
38
-static int ffm_is_avail_data(AVFormatContext *s, int size)
39
-{
40
-    FFMContext *ffm = s->priv_data;
41
-    int64_t pos, avail_size;
42
-    ptrdiff_t len;
43
-
44
-    len = ffm->packet_end - ffm->packet_ptr;
45
-    if (size <= len)
46
-        return 1;
47
-    pos = avio_tell(s->pb);
48
-    if (!ffm->write_index) {
49
-        if (pos == ffm->file_size)
50
-            return AVERROR_EOF;
51
-        avail_size = ffm->file_size - pos;
52
-    } else {
53
-    if (pos == ffm->write_index) {
54
-        /* exactly at the end of stream */
55
-        if (ffm->server_attached)
56
-            return AVERROR(EAGAIN);
57
-        else
58
-            return AVERROR_INVALIDDATA;
59
-    } else if (pos < ffm->write_index) {
60
-        avail_size = ffm->write_index - pos;
61
-    } else {
62
-        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
63
-    }
64
-    }
65
-    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
66
-    if (size <= avail_size)
67
-        return 1;
68
-    else if (ffm->server_attached)
69
-        return AVERROR(EAGAIN);
70
-    else
71
-        return AVERROR_INVALIDDATA;
72
-}
73
-
74
-static int ffm_resync(AVFormatContext *s, uint32_t state)
75
-{
76
-    av_log(s, AV_LOG_ERROR, "resyncing\n");
77
-    while (state != PACKET_ID) {
78
-        if (avio_feof(s->pb)) {
79
-            av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
80
-            return -1;
81
-        }
82
-        state = (state << 8) | avio_r8(s->pb);
83
-    }
84
-    return 0;
85
-}
86
-
87
-/* first is true if we read the frame header */
88
-static int ffm_read_data(AVFormatContext *s,
89
-                         uint8_t *buf, int size, int header)
90
-{
91
-    FFMContext *ffm = s->priv_data;
92
-    AVIOContext *pb = s->pb;
93
-    int fill_size, size1, frame_offset;
94
-    uint32_t id;
95
-    ptrdiff_t len;
96
-    int64_t last_pos = -1;
97
-
98
-    size1 = size;
99
-    while (size > 0) {
100
-    redo:
101
-        len = ffm->packet_end - ffm->packet_ptr;
102
-        if (len < 0)
103
-            return -1;
104
-        if (len > size)
105
-            len = size;
106
-        if (len == 0) {
107
-            if (avio_tell(pb) == ffm->file_size) {
108
-                if (ffm->server_attached) {
109
-                    avio_seek(pb, ffm->packet_size, SEEK_SET);
110
-                } else
111
-                    return AVERROR_EOF;
112
-            }
113
-    retry_read:
114
-            if (pb->buffer_size != ffm->packet_size) {
115
-                int64_t tell = avio_tell(pb);
116
-                int ret = ffio_set_buf_size(pb, ffm->packet_size);
117
-                if (ret < 0)
118
-                    return ret;
119
-                avio_seek(pb, tell, SEEK_SET);
120
-            }
121
-            id = avio_rb16(pb); /* PACKET_ID */
122
-            if (id != PACKET_ID) {
123
-                if (ffm_resync(s, id) < 0)
124
-                    return -1;
125
-                last_pos = avio_tell(pb);
126
-            }
127
-            fill_size = avio_rb16(pb);
128
-            ffm->dts = avio_rb64(pb);
129
-            frame_offset = avio_rb16(pb);
130
-            avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
131
-            if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
132
-                return -1;
133
-            }
134
-            ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
135
-            /* if first packet or resynchronization packet, we must
136
-               handle it specifically */
137
-            if (ffm->first_packet || (frame_offset & 0x8000)) {
138
-                if (!frame_offset) {
139
-                    /* This packet has no frame headers in it */
140
-                    if (avio_tell(pb) >= ffm->packet_size * 3LL) {
141
-                        int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
142
-                        seekback = FFMAX(seekback, 0);
143
-                        avio_seek(pb, -seekback, SEEK_CUR);
144
-                        goto retry_read;
145
-                    }
146
-                    /* This is bad, we cannot find a valid frame header */
147
-                    return 0;
148
-                }
149
-                ffm->first_packet = 0;
150
-                if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
151
-                    ffm->packet_end = ffm->packet_ptr;
152
-                    return -1;
153
-                }
154
-                ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
155
-                if (!header)
156
-                    break;
157
-            } else {
158
-                ffm->packet_ptr = ffm->packet;
159
-            }
160
-            goto redo;
161
-        }
162
-        memcpy(buf, ffm->packet_ptr, len);
163
-        buf += len;
164
-        ffm->packet_ptr += len;
165
-        size -= len;
166
-        header = 0;
167
-    }
168
-    return size1 - size;
169
-}
170
-
171
-/* ensure that actual seeking happens between FFM_PACKET_SIZE
172
-   and file_size - FFM_PACKET_SIZE */
173
-static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
174
-{
175
-    FFMContext *ffm = s->priv_data;
176
-    AVIOContext *pb = s->pb;
177
-    int64_t pos;
178
-
179
-    pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
180
-    pos = FFMAX(pos, FFM_PACKET_SIZE);
181
-    ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
182
-    return avio_seek(pb, pos, SEEK_SET);
183
-}
184
-
185
-static int64_t get_dts(AVFormatContext *s, int64_t pos)
186
-{
187
-    AVIOContext *pb = s->pb;
188
-    int64_t dts;
189
-
190
-    ffm_seek1(s, pos);
191
-    avio_skip(pb, 4);
192
-    dts = avio_rb64(pb);
193
-    ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
194
-    return dts;
195
-}
196
-
197
-static void adjust_write_index(AVFormatContext *s)
198
-{
199
-    FFMContext *ffm = s->priv_data;
200
-    AVIOContext *pb = s->pb;
201
-    int64_t pts;
202
-    //int64_t orig_write_index = ffm->write_index;
203
-    int64_t pos_min, pos_max;
204
-    int64_t pts_start;
205
-    int64_t ptr = avio_tell(pb);
206
-
207
-
208
-    pos_min = 0;
209
-    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
210
-
211
-    pts_start = get_dts(s, pos_min);
212
-
213
-    pts = get_dts(s, pos_max);
214
-
215
-    if (pts - 100000 > pts_start)
216
-        goto end;
217
-
218
-    ffm->write_index = FFM_PACKET_SIZE;
219
-
220
-    pts_start = get_dts(s, pos_min);
221
-
222
-    pts = get_dts(s, pos_max);
223
-
224
-    if (pts - 100000 <= pts_start) {
225
-        while (1) {
226
-            int64_t newpos;
227
-            int64_t newpts;
228
-
229
-            newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
230
-
231
-            if (newpos == pos_min)
232
-                break;
233
-
234
-            newpts = get_dts(s, newpos);
235
-
236
-            if (newpts - 100000 <= pts) {
237
-                pos_max = newpos;
238
-                pts = newpts;
239
-            } else {
240
-                pos_min = newpos;
241
-            }
242
-        }
243
-        ffm->write_index += pos_max;
244
-    }
245
-
246
- end:
247
-    avio_seek(pb, ptr, SEEK_SET);
248
-}
249
-
250
-
251
-static int ffm_append_recommended_configuration(AVStream *st, char **conf)
252
-{
253
-    int ret;
254
-    size_t newsize;
255
-    av_assert0(conf && st);
256
-    if (!*conf)
257
-        return 0;
258
-    if (!st->recommended_encoder_configuration) {
259
-        st->recommended_encoder_configuration = *conf;
260
-        *conf = 0;
261
-        return 0;
262
-    }
263
-    newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
264
-    if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
265
-        return ret;
266
-    av_strlcat(st->recommended_encoder_configuration, ",", newsize);
267
-    av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
268
-    av_freep(conf);
269
-    return 0;
270
-}
271
-
272
-#define VALIDATE_PARAMETER(parameter, name, check) {                              \
273
-    if (check) {                                                                  \
274
-        av_log(s, AV_LOG_ERROR, "Invalid " name " %d\n", codecpar->parameter);   \
275
-        ret = AVERROR_INVALIDDATA;                                                \
276
-        goto fail;                                                                \
277
-    }                                                                             \
278
-}
279
-
280
-static int ffm2_read_header(AVFormatContext *s)
281
-{
282
-    FFMContext *ffm = s->priv_data;
283
-    AVStream *st = NULL;
284
-    AVIOContext *pb = s->pb;
285
-    AVCodecContext *dummy_codec = NULL;
286
-    AVCodecParameters *codecpar = NULL;
287
-    const AVCodecDescriptor *codec_desc;
288
-    int ret;
289
-    int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
290
-    AVCodec *enc;
291
-    char *buffer;
292
-
293
-    ffm->packet_size = avio_rb32(pb);
294
-    if (ffm->packet_size != FFM_PACKET_SIZE) {
295
-        av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
296
-               ffm->packet_size, FFM_PACKET_SIZE);
297
-        ret = AVERROR_INVALIDDATA;
298
-        goto fail;
299
-    }
300
-
301
-    ffm->write_index = avio_rb64(pb);
302
-    /* get also filesize */
303
-    if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
304
-        ffm->file_size = avio_size(pb);
305
-        if (ffm->write_index && 0)
306
-            adjust_write_index(s);
307
-    } else {
308
-        ffm->file_size = (UINT64_C(1) << 63) - 1;
309
-    }
310
-    dummy_codec = avcodec_alloc_context3(NULL);
311
-
312
-    while(!avio_feof(pb)) {
313
-        unsigned id = avio_rb32(pb);
314
-        unsigned size = avio_rb32(pb);
315
-        int64_t next = avio_tell(pb) + size;
316
-        char rc_eq_buf[128];
317
-        int flags;
318
-
319
-        if(!id)
320
-            break;
321
-
322
-        switch(id) {
323
-        case MKBETAG('M', 'A', 'I', 'N'):
324
-            if (f_main++) {
325
-                ret = AVERROR(EINVAL);
326
-                goto fail;
327
-            }
328
-            avio_rb32(pb); /* nb_streams */
329
-            avio_rb32(pb); /* total bitrate */
330
-            break;
331
-        case MKBETAG('C', 'O', 'M', 'M'):
332
-            f_cprv = f_stvi = f_stau = 0;
333
-            st = avformat_new_stream(s, NULL);
334
-            if (!st) {
335
-                ret = AVERROR(ENOMEM);
336
-                goto fail;
337
-            }
338
-
339
-            avpriv_set_pts_info(st, 64, 1, 1000000);
340
-
341
-            codecpar = st->codecpar;
342
-            /* generic info */
343
-            codecpar->codec_id = avio_rb32(pb);
344
-            codec_desc = avcodec_descriptor_get(codecpar->codec_id);
345
-            if (!codec_desc) {
346
-                av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
347
-                codecpar->codec_id = AV_CODEC_ID_NONE;
348
-                ret = AVERROR_INVALIDDATA;
349
-                goto fail;
350
-            }
351
-            codecpar->codec_type = avio_r8(pb);
352
-            if (codecpar->codec_type != codec_desc->type) {
353
-                av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
354
-                       codec_desc->type, codecpar->codec_type);
355
-                codecpar->codec_id = AV_CODEC_ID_NONE;
356
-                codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
357
-                ret = AVERROR_INVALIDDATA;
358
-                goto fail;
359
-            }
360
-            codecpar->bit_rate = avio_rb32(pb);
361
-            if (codecpar->bit_rate < 0) {
362
-                av_log(s, AV_LOG_ERROR, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
363
-                ret = AVERROR_INVALIDDATA;
364
-                goto fail;
365
-            }
366
-            flags = avio_rb32(pb);
367
-#if FF_API_LAVF_AVCTX
368
-FF_DISABLE_DEPRECATION_WARNINGS
369
-            st->codec->flags = flags;
370
-FF_ENABLE_DEPRECATION_WARNINGS
371
-#endif
372
-            avio_rb32(pb); // flags2
373
-            avio_rb32(pb); // debug
374
-            if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
375
-                int size = avio_rb32(pb);
376
-                if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
377
-                    av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
378
-                    ret = AVERROR_INVALIDDATA;
379
-                    goto fail;
380
-                }
381
-                codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
382
-                if (!codecpar->extradata) {
383
-                    ret = AVERROR(ENOMEM);
384
-                    goto fail;
385
-                }
386
-                codecpar->extradata_size = size;
387
-                avio_read(pb, codecpar->extradata, size);
388
-            }
389
-            break;
390
-        case MKBETAG('S', 'T', 'V', 'I'):
391
-            if (f_stvi++ || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
392
-                ret = AVERROR(EINVAL);
393
-                goto fail;
394
-            }
395
-            avio_rb32(pb); // time_base.num
396
-            avio_rb32(pb); // time_base.den
397
-            codecpar->width = avio_rb16(pb);
398
-            codecpar->height = avio_rb16(pb);
399
-            ret = av_image_check_size(codecpar->width, codecpar->height, 0, s);
400
-            if (ret < 0)
401
-                goto fail;
402
-            avio_rb16(pb); // gop_size
403
-            codecpar->format = avio_rb32(pb);
404
-            if (!av_pix_fmt_desc_get(codecpar->format)) {
405
-                av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
406
-                codecpar->format = AV_PIX_FMT_NONE;
407
-                ret = AVERROR_INVALIDDATA;
408
-                goto fail;
409
-            }
410
-            avio_r8(pb);   // qmin
411
-            avio_r8(pb);   // qmax
412
-            avio_r8(pb);   // max_qdiff
413
-            avio_rb16(pb); // qcompress / 10000.0
414
-            avio_rb16(pb); // qblur / 10000.0
415
-            avio_rb32(pb); // bit_rate_tolerance
416
-            avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
417
-
418
-            avio_rb32(pb); // rc_max_rate
419
-            avio_rb32(pb); // rc_min_rate
420
-            avio_rb32(pb); // rc_buffer_size
421
-            avio_rb64(pb); // i_quant_factor
422
-            avio_rb64(pb); // b_quant_factor
423
-            avio_rb64(pb); // i_quant_offset
424
-            avio_rb64(pb); // b_quant_offset
425
-            avio_rb32(pb); // dct_algo
426
-            avio_rb32(pb); // strict_std_compliance
427
-            avio_rb32(pb); // max_b_frames
428
-            avio_rb32(pb); // mpeg_quant
429
-            avio_rb32(pb); // intra_dc_precision
430
-            avio_rb32(pb); // me_method
431
-            avio_rb32(pb); // mb_decision
432
-            avio_rb32(pb); // nsse_weight
433
-            avio_rb32(pb); // frame_skip_cmp
434
-            avio_rb64(pb); // rc_buffer_aggressivity
435
-            codecpar->codec_tag = avio_rb32(pb);
436
-            avio_r8(pb);   // thread_count
437
-            avio_rb32(pb); // coder_type
438
-            avio_rb32(pb); // me_cmp
439
-            avio_rb32(pb); // me_subpel_quality
440
-            avio_rb32(pb); // me_range
441
-            avio_rb32(pb); // keyint_min
442
-            avio_rb32(pb); // scenechange_threshold
443
-            avio_rb32(pb); // b_frame_strategy
444
-            avio_rb64(pb); // qcompress
445
-            avio_rb64(pb); // qblur
446
-            avio_rb32(pb); // max_qdiff
447
-            avio_rb32(pb); // refs
448
-            break;
449
-        case MKBETAG('S', 'T', 'A', 'U'):
450
-            if (f_stau++ || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
451
-                ret = AVERROR(EINVAL);
452
-                goto fail;
453
-            }
454
-            codecpar->sample_rate = avio_rb32(pb);
455
-            VALIDATE_PARAMETER(sample_rate, "sample rate",        codecpar->sample_rate < 0)
456
-            codecpar->channels = avio_rl16(pb);
457
-            VALIDATE_PARAMETER(channels,    "number of channels", codecpar->channels < 0)
458
-            codecpar->frame_size = avio_rl16(pb);
459
-            VALIDATE_PARAMETER(frame_size,  "frame size",         codecpar->frame_size < 0)
460
-            break;
461
-        case MKBETAG('C', 'P', 'R', 'V'):
462
-            if (f_cprv++) {
463
-                ret = AVERROR(EINVAL);
464
-                goto fail;
465
-            }
466
-            enc = avcodec_find_encoder(codecpar->codec_id);
467
-            if (enc && enc->priv_data_size && enc->priv_class) {
468
-                buffer = av_malloc(size + 1);
469
-                if (!buffer) {
470
-                    ret = AVERROR(ENOMEM);
471
-                    goto fail;
472
-                }
473
-                avio_get_str(pb, size, buffer, size + 1);
474
-                if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
475
-                    goto fail;
476
-            }
477
-            break;
478
-        case MKBETAG('S', '2', 'V', 'I'):
479
-            if (f_stvi++ || !size || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
480
-                ret = AVERROR(EINVAL);
481
-                goto fail;
482
-            }
483
-            buffer = av_malloc(size);
484
-            if (!buffer) {
485
-                ret = AVERROR(ENOMEM);
486
-                goto fail;
487
-            }
488
-            avio_get_str(pb, INT_MAX, buffer, size);
489
-            // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
490
-            avcodec_parameters_to_context(dummy_codec, codecpar);
491
-            av_set_options_string(dummy_codec, buffer, "=", ",");
492
-            avcodec_parameters_from_context(codecpar, dummy_codec);
493
-            if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
494
-                goto fail;
495
-            break;
496
-        case MKBETAG('S', '2', 'A', 'U'):
497
-            if (f_stau++ || !size || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
498
-                ret = AVERROR(EINVAL);
499
-                goto fail;
500
-            }
501
-            buffer = av_malloc(size);
502
-            if (!buffer) {
503
-                ret = AVERROR(ENOMEM);
504
-                goto fail;
505
-            }
506
-            avio_get_str(pb, INT_MAX, buffer, size);
507
-            // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
508
-            avcodec_parameters_to_context(dummy_codec, codecpar);
509
-            av_set_options_string(dummy_codec, buffer, "=", ",");
510
-            avcodec_parameters_from_context(codecpar, dummy_codec);
511
-            if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
512
-                goto fail;
513
-            break;
514
-        }
515
-        avio_seek(pb, next, SEEK_SET);
516
-    }
517
-
518
-    /* get until end of block reached */
519
-    while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
520
-        avio_r8(pb);
521
-
522
-    /* init packet demux */
523
-    ffm->packet_ptr = ffm->packet;
524
-    ffm->packet_end = ffm->packet;
525
-    ffm->frame_offset = 0;
526
-    ffm->dts = 0;
527
-    ffm->read_state = READ_HEADER;
528
-    ffm->first_packet = 1;
529
-    avcodec_free_context(&dummy_codec);
530
-    return 0;
531
- fail:
532
-    avcodec_free_context(&dummy_codec);
533
-    return ret;
534
-}
535
-
536
-static int ffm_read_header(AVFormatContext *s)
537
-{
538
-    FFMContext *ffm = s->priv_data;
539
-    AVStream *st;
540
-    AVIOContext *pb = s->pb;
541
-    AVCodecContext *dummy_codec = NULL;
542
-    AVCodecParameters *codecpar;
543
-    const AVCodecDescriptor *codec_desc;
544
-    int i, nb_streams, ret;
545
-    uint32_t tag;
546
-
547
-    /* header */
548
-    tag = avio_rl32(pb);
549
-    if (tag == MKTAG('F', 'F', 'M', '2'))
550
-        return ffm2_read_header(s);
551
-    if (tag != MKTAG('F', 'F', 'M', '1')) {
552
-        ret = AVERROR_INVALIDDATA;
553
-        goto fail;
554
-    }
555
-    ffm->packet_size = avio_rb32(pb);
556
-    if (ffm->packet_size != FFM_PACKET_SIZE) {
557
-        ret = AVERROR_INVALIDDATA;
558
-        goto fail;
559
-    }
560
-    ffm->write_index = avio_rb64(pb);
561
-    /* get also filesize */
562
-    if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
563
-        ffm->file_size = avio_size(pb);
564
-        if (ffm->write_index && 0)
565
-            adjust_write_index(s);
566
-    } else {
567
-        ffm->file_size = (UINT64_C(1) << 63) - 1;
568
-    }
569
-    dummy_codec = avcodec_alloc_context3(NULL);
570
-
571
-    nb_streams = avio_rb32(pb);
572
-    avio_rb32(pb); /* total bitrate */
573
-    /* read each stream */
574
-    for(i=0;i<nb_streams;i++) {
575
-        char rc_eq_buf[128];
576
-        int flags;
577
-
578
-        st = avformat_new_stream(s, NULL);
579
-        if (!st) {
580
-            ret = AVERROR(ENOMEM);
581
-            goto fail;
582
-        }
583
-
584
-        avpriv_set_pts_info(st, 64, 1, 1000000);
585
-
586
-        codecpar = st->codecpar;
587
-        /* generic info */
588
-        codecpar->codec_id = avio_rb32(pb);
589
-        codec_desc = avcodec_descriptor_get(codecpar->codec_id);
590
-        if (!codec_desc) {
591
-            av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
592
-            codecpar->codec_id = AV_CODEC_ID_NONE;
593
-            ret = AVERROR_INVALIDDATA;
594
-            goto fail;
595
-        }
596
-        codecpar->codec_type = avio_r8(pb); /* codec_type */
597
-        if (codecpar->codec_type != codec_desc->type) {
598
-            av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
599
-                   codec_desc->type, codecpar->codec_type);
600
-            codecpar->codec_id = AV_CODEC_ID_NONE;
601
-            codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
602
-            ret = AVERROR_INVALIDDATA;
603
-            goto fail;
604
-        }
605
-        codecpar->bit_rate = avio_rb32(pb);
606
-        if (codecpar->bit_rate < 0) {
607
-            av_log(s, AV_LOG_WARNING, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
608
-            ret = AVERROR_INVALIDDATA;
609
-            goto fail;
610
-        }
611
-        flags = avio_rb32(pb);
612
-#if FF_API_LAVF_AVCTX
613
-FF_DISABLE_DEPRECATION_WARNINGS
614
-            st->codec->flags = flags;
615
-FF_ENABLE_DEPRECATION_WARNINGS
616
-#endif
617
-        avio_rb32(pb); // flags2
618
-        avio_rb32(pb); // debug
619
-        /* specific info */
620
-        switch(codecpar->codec_type) {
621
-        case AVMEDIA_TYPE_VIDEO:
622
-            avio_rb32(pb); // time_base.num
623
-            avio_rb32(pb); // time_base.den
624
-            codecpar->width = avio_rb16(pb);
625
-            codecpar->height = avio_rb16(pb);
626
-            if ((ret = av_image_check_size(codecpar->width, codecpar->height, 0, s)) < 0)
627
-                goto fail;
628
-            avio_rb16(pb); // gop_size
629
-            codecpar->format = avio_rb32(pb);
630
-            if (!av_pix_fmt_desc_get(codecpar->format)) {
631
-                av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
632
-                codecpar->format = AV_PIX_FMT_NONE;
633
-                ret = AVERROR_INVALIDDATA;
634
-                goto fail;
635
-            }
636
-            avio_r8(pb);   // qmin
637
-            avio_r8(pb);   // qmax
638
-            avio_r8(pb);   // max_qdiff
639
-            avio_rb16(pb); // qcompress / 10000.0
640
-            avio_rb16(pb); // qblur / 10000.0
641
-            avio_rb32(pb); // bit_rate_tolerance
642
-            avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
643
-
644
-            avio_rb32(pb); // rc_max_rate
645
-            avio_rb32(pb); // rc_min_rate
646
-            avio_rb32(pb); // rc_buffer_size
647
-            avio_rb64(pb); // i_quant_factor
648
-            avio_rb64(pb); // b_quant_factor
649
-            avio_rb64(pb); // i_quant_offset
650
-            avio_rb64(pb); // b_quant_offset
651
-            avio_rb32(pb); // dct_algo
652
-            avio_rb32(pb); // strict_std_compliance
653
-            avio_rb32(pb); // max_b_frames
654
-            avio_rb32(pb); // mpeg_quant
655
-            avio_rb32(pb); // intra_dc_precision
656
-            avio_rb32(pb); // me_method
657
-            avio_rb32(pb); // mb_decision
658
-            avio_rb32(pb); // nsse_weight
659
-            avio_rb32(pb); // frame_skip_cmp
660
-            avio_rb64(pb); // rc_buffer_aggressivity
661
-            codecpar->codec_tag = avio_rb32(pb);
662
-            avio_r8(pb);   // thread_count
663
-            avio_rb32(pb); // coder_type
664
-            avio_rb32(pb); // me_cmp
665
-            avio_rb32(pb); // me_subpel_quality
666
-            avio_rb32(pb); // me_range
667
-            avio_rb32(pb); // keyint_min
668
-            avio_rb32(pb); // scenechange_threshold
669
-            avio_rb32(pb); // b_frame_strategy
670
-            avio_rb64(pb); // qcompress
671
-            avio_rb64(pb); // qblur
672
-            avio_rb32(pb); // max_qdiff
673
-            avio_rb32(pb); // refs
674
-            break;
675
-        case AVMEDIA_TYPE_AUDIO:
676
-            codecpar->sample_rate = avio_rb32(pb);
677
-            VALIDATE_PARAMETER(sample_rate, "sample rate",        codecpar->sample_rate < 0)
678
-            codecpar->channels = avio_rl16(pb);
679
-            VALIDATE_PARAMETER(channels,    "number of channels", codecpar->channels < 0)
680
-            codecpar->frame_size = avio_rl16(pb);
681
-            VALIDATE_PARAMETER(frame_size,  "frame size",         codecpar->frame_size < 0)
682
-            break;
683
-        default:
684
-            ret = AVERROR_INVALIDDATA;
685
-            goto fail;
686
-        }
687
-        if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
688
-            int size = avio_rb32(pb);
689
-            if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
690
-                av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
691
-                ret = AVERROR_INVALIDDATA;
692
-                goto fail;
693
-            }
694
-            codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
695
-            if (!codecpar->extradata) {
696
-                ret = AVERROR(ENOMEM);
697
-                goto fail;
698
-            }
699
-            codecpar->extradata_size = size;
700
-            avio_read(pb, codecpar->extradata, size);
701
-        }
702
-    }
703
-
704
-    /* get until end of block reached */
705
-    while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
706
-        avio_r8(pb);
707
-
708
-    /* init packet demux */
709
-    ffm->packet_ptr = ffm->packet;
710
-    ffm->packet_end = ffm->packet;
711
-    ffm->frame_offset = 0;
712
-    ffm->dts = 0;
713
-    ffm->read_state = READ_HEADER;
714
-    ffm->first_packet = 1;
715
-    avcodec_free_context(&dummy_codec);
716
-    return 0;
717
- fail:
718
-    avcodec_free_context(&dummy_codec);
719
-    return ret;
720
-}
721
-
722
-/* return < 0 if eof */
723
-static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
724
-{
725
-    int size;
726
-    FFMContext *ffm = s->priv_data;
727
-    int duration, ret;
728
-
729
-    switch(ffm->read_state) {
730
-    case READ_HEADER:
731
-        if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
732
-            return ret;
733
-
734
-        ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
735
-               avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
736
-        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
737
-            FRAME_HEADER_SIZE)
738
-            return -1;
739
-        if (ffm->header[1] & FLAG_DTS)
740
-            if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
741
-                return -1;
742
-        ffm->read_state = READ_DATA;
743
-        /* fall through */
744
-    case READ_DATA:
745
-        size = AV_RB24(ffm->header + 2);
746
-        if ((ret = ffm_is_avail_data(s, size)) < 0)
747
-            return ret;
748
-
749
-        duration = AV_RB24(ffm->header + 5);
750
-
751
-        if (av_new_packet(pkt, size) < 0) {
752
-            return AVERROR(ENOMEM);
753
-        }
754
-        pkt->stream_index = ffm->header[0];
755
-        if ((unsigned)pkt->stream_index >= s->nb_streams) {
756
-            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
757
-            av_packet_unref(pkt);
758
-            ffm->read_state = READ_HEADER;
759
-            return -1;
760
-        }
761
-        pkt->pos = avio_tell(s->pb);
762
-        if (ffm->header[1] & FLAG_KEY_FRAME)
763
-            pkt->flags |= AV_PKT_FLAG_KEY;
764
-
765
-        ffm->read_state = READ_HEADER;
766
-        if (ffm_read_data(s, pkt->data, size, 0) != size) {
767
-            /* bad case: desynchronized packet. we cancel all the packet loading */
768
-            av_packet_unref(pkt);
769
-            return -1;
770
-        }
771
-        pkt->pts = AV_RB64(ffm->header+8);
772
-        if (ffm->header[1] & FLAG_DTS)
773
-            pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
774
-        else
775
-            pkt->dts = pkt->pts;
776
-        pkt->duration = duration;
777
-        break;
778
-    }
779
-    return 0;
780
-}
781
-
782
-/* seek to a given time in the file. The file read pointer is
783
-   positioned at or before pts. XXX: the following code is quite
784
-   approximative */
785
-static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
786
-{
787
-    FFMContext *ffm = s->priv_data;
788
-    int64_t pos_min, pos_max, pos;
789
-    int64_t pts_min, pts_max, pts;
790
-    double pos1;
791
-
792
-    ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
793
-    /* find the position using linear interpolation (better than
794
-       dichotomy in typical cases) */
795
-    if (ffm->write_index && ffm->write_index < ffm->file_size) {
796
-        if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
797
-            pos_min = FFM_PACKET_SIZE;
798
-            pos_max = ffm->write_index - FFM_PACKET_SIZE;
799
-        } else {
800
-            pos_min = ffm->write_index;
801
-            pos_max = ffm->file_size - FFM_PACKET_SIZE;
802
-        }
803
-    } else {
804
-        pos_min = FFM_PACKET_SIZE;
805
-        pos_max = ffm->file_size - FFM_PACKET_SIZE;
806
-    }
807
-    while (pos_min <= pos_max) {
808
-        pts_min = get_dts(s, pos_min);
809
-        pts_max = get_dts(s, pos_max);
810
-        if (pts_min > wanted_pts || pts_max <= wanted_pts) {
811
-            pos = pts_min > wanted_pts ? pos_min : pos_max;
812
-            goto found;
813
-        }
814
-        /* linear interpolation */
815
-        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
816
-            (double)(pts_max - pts_min);
817
-        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
818
-        if (pos <= pos_min)
819
-            pos = pos_min;
820
-        else if (pos >= pos_max)
821
-            pos = pos_max;
822
-        pts = get_dts(s, pos);
823
-        /* check if we are lucky */
824
-        if (pts == wanted_pts) {
825
-            goto found;
826
-        } else if (pts > wanted_pts) {
827
-            pos_max = pos - FFM_PACKET_SIZE;
828
-        } else {
829
-            pos_min = pos + FFM_PACKET_SIZE;
830
-        }
831
-    }
832
-    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
833
-
834
- found:
835
-    if (ffm_seek1(s, pos) < 0)
836
-        return -1;
837
-
838
-    /* reset read state */
839
-    ffm->read_state = READ_HEADER;
840
-    ffm->packet_ptr = ffm->packet;
841
-    ffm->packet_end = ffm->packet;
842
-    ffm->first_packet = 1;
843
-
844
-    return 0;
845
-}
846
-
847
-static int ffm_probe(AVProbeData *p)
848
-{
849
-    if (
850
-        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
851
-        (p->buf[3] == '1' || p->buf[3] == '2'))
852
-        return AVPROBE_SCORE_MAX + 1;
853
-    return 0;
854
-}
855
-
856
-static const AVOption options[] = {
857
-    {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
858
-    {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
859
-    {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
860
-    { NULL },
861
-};
862
-
863
-static const AVClass ffm_class = {
864
-    .class_name = "ffm demuxer",
865
-    .item_name  = av_default_item_name,
866
-    .option     = options,
867
-    .version    = LIBAVUTIL_VERSION_INT,
868
-};
869
-AVInputFormat ff_ffm_demuxer = {
870
-    .name           = "ffm",
871
-    .long_name      = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
872
-    .priv_data_size = sizeof(FFMContext),
873
-    .read_probe     = ffm_probe,
874
-    .read_header    = ffm_read_header,
875
-    .read_packet    = ffm_read_packet,
876
-    .read_seek      = ffm_seek,
877
-    .priv_class     = &ffm_class,
878
-};
879 1
deleted file mode 100644
... ...
@@ -1,362 +0,0 @@
1
-/*
2
- * FFM (ffserver live feed) muxer
3
- * Copyright (c) 2001 Fabrice Bellard
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 "libavutil/intreadwrite.h"
23
-#include "libavutil/intfloat.h"
24
-#include "libavutil/avassert.h"
25
-#include "libavutil/parseutils.h"
26
-#include "libavutil/opt.h"
27
-#include "avformat.h"
28
-#include "avio_internal.h"
29
-#include "internal.h"
30
-#include "ffm.h"
31
-
32
-static void flush_packet(AVFormatContext *s)
33
-{
34
-    FFMContext *ffm = s->priv_data;
35
-    int fill_size, h;
36
-    AVIOContext *pb = s->pb;
37
-
38
-    fill_size = ffm->packet_end - ffm->packet_ptr;
39
-    memset(ffm->packet_ptr, 0, fill_size);
40
-
41
-    av_assert1(avio_tell(pb) % ffm->packet_size == 0);
42
-
43
-    /* put header */
44
-    avio_wb16(pb, PACKET_ID);
45
-    avio_wb16(pb, fill_size);
46
-    avio_wb64(pb, ffm->dts);
47
-    h = ffm->frame_offset;
48
-    if (ffm->first_packet)
49
-        h |= 0x8000;
50
-    avio_wb16(pb, h);
51
-    avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
52
-    avio_flush(pb);
53
-
54
-    /* prepare next packet */
55
-    ffm->frame_offset = 0; /* no key frame */
56
-    ffm->packet_ptr = ffm->packet;
57
-    ffm->first_packet = 0;
58
-}
59
-
60
-/* 'first' is true if first data of a frame */
61
-static void ffm_write_data(AVFormatContext *s,
62
-                           const uint8_t *buf, int size,
63
-                           int64_t dts, int header)
64
-{
65
-    FFMContext *ffm = s->priv_data;
66
-    int len;
67
-
68
-    if (header && ffm->frame_offset == 0) {
69
-        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
70
-        ffm->dts = dts;
71
-    }
72
-
73
-    /* write as many packets as needed */
74
-    while (size > 0) {
75
-        len = ffm->packet_end - ffm->packet_ptr;
76
-        if (len > size)
77
-            len = size;
78
-        memcpy(ffm->packet_ptr, buf, len);
79
-
80
-        ffm->packet_ptr += len;
81
-        buf += len;
82
-        size -= len;
83
-        if (ffm->packet_ptr >= ffm->packet_end)
84
-            flush_packet(s);
85
-    }
86
-}
87
-
88
-static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
89
-{
90
-    uint8_t *dyn_buf;
91
-    int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
92
-    avio_wb32(pb, id);
93
-    avio_wb32(pb, dyn_size);
94
-    avio_write(pb, dyn_buf, dyn_size);
95
-    av_free(dyn_buf);
96
-}
97
-
98
-static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecParameters *ctxpar, unsigned tag, int type)
99
-{
100
-    AVIOContext *tmp;
101
-    char *buf = NULL;
102
-    int ret, need_coma = 0;
103
-    AVCodecContext *ctx = NULL;
104
-
105
-#define SKIP_DEFAULTS   AV_OPT_SERIALIZE_SKIP_DEFAULTS
106
-#define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
107
-#define ENC             AV_OPT_FLAG_ENCODING_PARAM
108
-
109
-    if (avio_open_dyn_buf(&tmp) < 0)
110
-        return AVERROR(ENOMEM);
111
-
112
-    // AVCodecParameters does not suport AVOptions, we thus must copy it over to a context that does
113
-    // otherwise it could be used directly and this would be much simpler
114
-    ctx = avcodec_alloc_context3(NULL);
115
-    if (!ctx) {
116
-        ret = AVERROR(ENOMEM);
117
-        goto fail;
118
-    }
119
-    avcodec_parameters_to_context(ctx, ctxpar);
120
-
121
-    if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0)
122
-        goto fail;
123
-    if (buf && strlen(buf)) {
124
-        avio_write(tmp, buf, strlen(buf));
125
-        av_freep(&buf);
126
-        need_coma = 1;
127
-    }
128
-    if ((ret = av_opt_serialize(ctx, 0, SKIP_DEFAULTS | OPT_FLAGS_EXACT, &buf, '=', ',')) < 0)
129
-        goto fail;
130
-    if (buf && strlen(buf)) {
131
-        if (need_coma)
132
-            avio_w8(tmp, ',');
133
-        avio_write(tmp, buf, strlen(buf));
134
-    }
135
-    av_freep(&buf);
136
-    avio_w8(tmp, 0);
137
-    write_header_chunk(pb, tmp, tag);
138
-    avcodec_free_context(&ctx);
139
-    return 0;
140
-  fail:
141
-    av_free(buf);
142
-    ffio_free_dyn_buf(&tmp);
143
-    avcodec_free_context(&ctx);
144
-    return ret;
145
-
146
-#undef SKIP_DEFAULTS
147
-#undef OPT_FLAGS_EXACT
148
-#undef ENC
149
-}
150
-
151
-static int ffm_write_recommended_config(AVIOContext *pb, AVCodecParameters *codecpar, unsigned tag,
152
-                                        const char *configuration)
153
-{
154
-    int ret;
155
-    const AVCodec *enc = avcodec_find_encoder(codecpar->codec_id);
156
-    AVIOContext *tmp;
157
-    AVDictionaryEntry *t = NULL;
158
-    AVDictionary *all = NULL, *comm = NULL, *prv = NULL;
159
-    char *buf = NULL;
160
-
161
-    if (!enc || !enc->priv_class || !enc->priv_data_size) {
162
-        /* codec is not known/has no private options, so save everything as common options */
163
-        if (avio_open_dyn_buf(&tmp) < 0)
164
-            return AVERROR(ENOMEM);
165
-        avio_put_str(tmp, configuration);
166
-        write_header_chunk(pb, tmp, tag);
167
-        return 0;
168
-    }
169
-
170
-    if ((ret = av_dict_parse_string(&all, configuration, "=", ",", 0)) < 0)
171
-        return ret;
172
-
173
-    while ((t = av_dict_get(all, "", t, AV_DICT_IGNORE_SUFFIX))) {
174
-        if (av_opt_find((void *)&enc->priv_class, t->key, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
175
-            if ((ret = av_dict_set(&prv, t->key, t->value, 0)) < 0)
176
-                goto fail;
177
-        } else if ((ret = av_dict_set(&comm, t->key, t->value, 0)) < 0)
178
-            goto fail;
179
-    }
180
-
181
-    if (comm) {
182
-        if ((ret = av_dict_get_string(comm, &buf, '=', ',')) < 0 ||
183
-            (ret = avio_open_dyn_buf(&tmp)) < 0)
184
-            goto fail;
185
-        avio_put_str(tmp, buf);
186
-        av_freep(&buf);
187
-        write_header_chunk(pb, tmp, tag);
188
-    }
189
-    if (prv) {
190
-        if ((ret = av_dict_get_string(prv, &buf, '=', ',')) < 0 ||
191
-            (ret = avio_open_dyn_buf(&tmp)) < 0)
192
-            goto fail;
193
-        avio_put_str(tmp, buf);
194
-        write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
195
-    }
196
-
197
-  fail:
198
-    av_free(buf);
199
-    av_dict_free(&all);
200
-    av_dict_free(&comm);
201
-    av_dict_free(&prv);
202
-    return ret;
203
-}
204
-
205
-static int ffm_write_header(AVFormatContext *s)
206
-{
207
-    FFMContext *ffm = s->priv_data;
208
-    AVStream *st;
209
-    AVIOContext *pb = s->pb;
210
-    AVCodecParameters *codecpar;
211
-    int bit_rate, i, ret;
212
-
213
-    if ((ret = ff_parse_creation_time_metadata(s, &ffm->start_time, 0)) < 0)
214
-        return ret;
215
-
216
-    ffm->packet_size = FFM_PACKET_SIZE;
217
-
218
-    /* header */
219
-    avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
220
-    avio_wb32(pb, ffm->packet_size);
221
-    avio_wb64(pb, 0); /* current write position */
222
-
223
-    if(avio_open_dyn_buf(&pb) < 0)
224
-        return AVERROR(ENOMEM);
225
-
226
-    avio_wb32(pb, s->nb_streams);
227
-    bit_rate = 0;
228
-    for(i=0;i<s->nb_streams;i++) {
229
-        st = s->streams[i];
230
-        bit_rate += st->codecpar->bit_rate;
231
-    }
232
-    avio_wb32(pb, bit_rate);
233
-
234
-    write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
235
-
236
-    /* list of streams */
237
-    for(i=0;i<s->nb_streams;i++) {
238
-        int flags = 0;
239
-        st = s->streams[i];
240
-        avpriv_set_pts_info(st, 64, 1, 1000000);
241
-        if(avio_open_dyn_buf(&pb) < 0)
242
-            return AVERROR(ENOMEM);
243
-
244
-        codecpar = st->codecpar;
245
-        /* generic info */
246
-        avio_wb32(pb, codecpar->codec_id);
247
-        avio_w8(pb, codecpar->codec_type);
248
-        avio_wb32(pb, codecpar->bit_rate);
249
-        if (codecpar->extradata_size)
250
-            flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
251
-
252
-        // If the user is not providing us with a configuration we have to fill it in as we cannot access the encoder
253
-        if (!st->recommended_encoder_configuration) {
254
-            if (s->flags & AVFMT_FLAG_BITEXACT)
255
-                flags |= AV_CODEC_FLAG_BITEXACT;
256
-        }
257
-
258
-        avio_wb32(pb, flags);
259
-        avio_wb32(pb, 0); // flags2
260
-        avio_wb32(pb, 0); // debug
261
-        if (codecpar->extradata_size) {
262
-            avio_wb32(pb, codecpar->extradata_size);
263
-            avio_write(pb, codecpar->extradata, codecpar->extradata_size);
264
-        }
265
-        write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
266
-        /* specific info */
267
-        switch(codecpar->codec_type) {
268
-        case AVMEDIA_TYPE_VIDEO:
269
-            if (st->recommended_encoder_configuration) {
270
-                av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
271
-                       st->recommended_encoder_configuration);
272
-                if ((ret = ffm_write_recommended_config(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'),
273
-                                                        st->recommended_encoder_configuration)) < 0)
274
-                return ret;
275
-            } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0)
276
-                return ret;
277
-            break;
278
-        case AVMEDIA_TYPE_AUDIO:
279
-            if (st->recommended_encoder_configuration) {
280
-                av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
281
-                       st->recommended_encoder_configuration);
282
-                if ((ret = ffm_write_recommended_config(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'),
283
-                                                        st->recommended_encoder_configuration)) < 0)
284
-                return ret;
285
-            } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0)
286
-                return ret;
287
-            break;
288
-        default:
289
-            return -1;
290
-        }
291
-    }
292
-    pb = s->pb;
293
-
294
-    avio_wb64(pb, 0); // end of header
295
-
296
-    /* flush until end of block reached */
297
-    while ((avio_tell(pb) % ffm->packet_size) != 0)
298
-        avio_w8(pb, 0);
299
-
300
-    avio_flush(pb);
301
-
302
-    /* init packet mux */
303
-    ffm->packet_ptr = ffm->packet;
304
-    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
305
-    av_assert0(ffm->packet_end >= ffm->packet);
306
-    ffm->frame_offset = 0;
307
-    ffm->dts = 0;
308
-    ffm->first_packet = 1;
309
-
310
-    return 0;
311
-}
312
-
313
-static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
314
-{
315
-    FFMContext *ffm = s->priv_data;
316
-    int64_t dts;
317
-    uint8_t header[FRAME_HEADER_SIZE+4];
318
-    int header_size = FRAME_HEADER_SIZE;
319
-
320
-    dts = ffm->start_time + pkt->dts;
321
-    /* packet size & key_frame */
322
-    header[0] = pkt->stream_index;
323
-    header[1] = 0;
324
-    if (pkt->flags & AV_PKT_FLAG_KEY)
325
-        header[1] |= FLAG_KEY_FRAME;
326
-    AV_WB24(header+2, pkt->size);
327
-    AV_WB24(header+5, pkt->duration);
328
-    AV_WB64(header+8, ffm->start_time + pkt->pts);
329
-    if (pkt->pts != pkt->dts) {
330
-        header[1] |= FLAG_DTS;
331
-        AV_WB32(header+16, pkt->pts - pkt->dts);
332
-        header_size += 4;
333
-    }
334
-    ffm_write_data(s, header, header_size, dts, 1);
335
-    ffm_write_data(s, pkt->data, pkt->size, dts, 0);
336
-
337
-    return 0;
338
-}
339
-
340
-static int ffm_write_trailer(AVFormatContext *s)
341
-{
342
-    FFMContext *ffm = s->priv_data;
343
-
344
-    /* flush packets */
345
-    if (ffm->packet_ptr > ffm->packet)
346
-        flush_packet(s);
347
-
348
-    return 0;
349
-}
350
-
351
-AVOutputFormat ff_ffm_muxer = {
352
-    .name              = "ffm",
353
-    .long_name         = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
354
-    .extensions        = "ffm",
355
-    .priv_data_size    = sizeof(FFMContext),
356
-    .audio_codec       = AV_CODEC_ID_MP2,
357
-    .video_codec       = AV_CODEC_ID_MPEG1VIDEO,
358
-    .write_header      = ffm_write_header,
359
-    .write_packet      = ffm_write_packet,
360
-    .write_trailer     = ffm_write_trailer,
361
-    .flags             = AVFMT_TS_NEGATIVE,
362
-};