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
34
36 {
38 int64_t pos, avail_size;
40
42 if (size <= len)
43 return 1;
49 } else {
51 /* exactly at the end of stream */
54 else
58 } else {
60 }
61 }
63 if (size <= avail_size)
64 return 1;
67 else
69 }
70
72 {
77 return -1;
78 }
80 }
81 return 0;
82 }
83
84 /* first is true if we read the frame header */
87 {
90 int fill_size, size1, frame_offset;
93 int64_t last_pos = -1;
94
96 while (size > 0) {
97 redo:
99 if (len < 0)
100 return -1;
101 if (len > size)
103 if (len == 0) {
107 } else
109 }
110 retry_read:
114 if (ret < 0)
115 return ret;
117 }
121 return -1;
123 }
129 return -1;
130 }
132 /* if first packet or resynchronization packet, we must
133 handle it specifically */
135 if (!frame_offset) {
136 /* This packet has no frame headers in it */
139 seekback =
FFMAX(seekback, 0);
141 goto retry_read;
142 }
143 /* This is bad, we cannot find a valid frame header */
144 return 0;
145 }
149 return -1;
150 }
152 if (!header)
153 break;
154 } else {
156 }
157 goto redo;
158 }
163 header = 0;
164 }
166 }
167
168 /* ensure that actual seeking happens between FFM_PACKET_SIZE
169 and file_size - FFM_PACKET_SIZE */
171 {
174 int64_t pos;
175
178 ff_dlog(s,
"seek to %"PRIx64
" -> %"PRIx64
"\n", pos1, pos);
180 }
181
183 {
185 int64_t dts;
186
190 ff_dlog(s,
"dts=%0.6f\n", dts / 1000000.0);
191 return dts;
192 }
193
195 {
199 //int64_t orig_write_index = ffm->write_index;
200 int64_t pos_min, pos_max;
201 int64_t pts_start;
203
204
205 pos_min = 0;
207
208 pts_start =
get_dts(s, pos_min);
209
211
212 if (pts - 100000 > pts_start)
214
216
217 pts_start =
get_dts(s, pos_min);
218
220
221 if (pts - 100000 <= pts_start) {
222 while (1) {
223 int64_t newpos;
224 int64_t newpts;
225
227
228 if (newpos == pos_min)
229 break;
230
232
233 if (newpts - 100000 <= pts) {
234 pos_max = newpos;
235 pts = newpts;
236 } else {
237 pos_min = newpos;
238 }
239 }
241 }
242
245 }
246
247
249 {
250 int i;
251
254
255 return 0;
256 }
257
259 {
260 int ret;
261 size_t newsize;
263 if (!*conf)
264 return 0;
267 *conf = 0;
268 return 0;
269 }
272 return ret;
276 return 0;
277 }
278
280 {
286 int ret;
287 int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
290
297 }
298
300 /* get also filesize */
305 } else {
306 ffm->
file_size = (UINT64_C(1) << 63) - 1;
307 }
308
313 char rc_eq_buf[128];
314
315 if(!id)
316 break;
317
318 switch(id) {
319 case MKBETAG(
'M',
'A',
'I',
'N'):
320 if (f_main++) {
323 }
326 break;
327 case MKBETAG(
'C',
'O',
'M',
'M'):
328 f_cprv = f_stvi = f_stau = 0;
330 if (!st) {
333 }
334
336
338 /* generic info */
341 if (!codec_desc) {
345 }
353 }
361 }
362 break;
363 case MKBETAG(
'S',
'T',
'V',
'I'):
364 if (f_stvi++) {
367 }
375 }
386 avio_get_str(pb, INT_MAX, rc_eq_buf,
sizeof(rc_eq_buf));
418 break;
419 case MKBETAG(
'S',
'T',
'A',
'U'):
420 if (f_stau++) {
423 }
427 break;
428 case MKBETAG(
'C',
'P',
'R',
'V'):
429 if (f_cprv++) {
432 }
436 if (!buffer) {
439 }
443 }
444 break;
445 case MKBETAG(
'S',
'2',
'V',
'I'):
446 if (f_stvi++ || !size) {
449 }
451 if (!buffer) {
454 }
459 break;
460 case MKBETAG(
'S',
'2',
'A',
'U'):
461 if (f_stau++ || !size) {
464 }
466 if (!buffer) {
469 }
474 break;
475 }
477 }
478
479 /* get until end of block reached */
482
483 /* init packet demux */
490 return 0;
493 return ret;
494 }
495
497 {
505
506 /* header */
508 if (tag ==
MKTAG(
'F',
'F',
'M',
'2'))
510 if (tag !=
MKTAG(
'F',
'F',
'M',
'1'))
516 /* get also filesize */
521 } else {
522 ffm->
file_size = (UINT64_C(1) << 63) - 1;
523 }
524
527 /* read each stream */
529 char rc_eq_buf[128];
530
532 if (!st)
534
536
538 /* generic info */
541 if (!codec_desc) {
545 }
553 }
558 /* specific info */
567 }
578 avio_get_str(pb, INT_MAX, rc_eq_buf,
sizeof(rc_eq_buf));
610 break;
615 break;
616 default:
618 }
622 }
623 }
624
625 /* get until end of block reached */
628
629 /* init packet demux */
636 return 0;
639 return -1;
640 }
641
642 /* return < 0 if eof */
644 {
648
652 return ret;
653
654 ff_dlog(s,
"pos=%08"PRIx64
" spos=%"PRIx64
", write_index=%"PRIx64
" size=%"PRIx64
"\n",
658 return -1;
661 return -1;
663 /* fall through */
667 return ret;
668
670
673 }
679 return -1;
680 }
684
687 /* bad case: desynchronized packet. we cancel all the packet loading */
689 return -1;
690 }
694 else
697 break;
698 }
699 return 0;
700 }
701
702 /* seek to a given time in the file. The file read pointer is
703 positioned at or before pts. XXX: the following code is quite
704 approximative */
706 {
708 int64_t pos_min, pos_max, pos;
709 int64_t pts_min, pts_max,
pts;
710 double pos1;
711
712 ff_dlog(s,
"wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
713 /* find the position using linear interpolation (better than
714 dichotomy in typical cases) */
719 } else {
722 }
723 } else {
726 }
727 while (pos_min <= pos_max) {
730 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
731 pos = pts_min > wanted_pts ? pos_min : pos_max;
732 goto found;
733 }
734 /* linear interpolation */
735 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
736 (double)(pts_max - pts_min);
738 if (pos <= pos_min)
739 pos = pos_min;
740 else if (pos >= pos_max)
741 pos = pos_max;
743 /* check if we are lucky */
744 if (pts == wanted_pts) {
745 goto found;
746 } else if (pts > wanted_pts) {
748 } else {
750 }
751 }
753
754 found:
756 return -1;
757
758 /* reset read state */
763
764 return 0;
765 }
766
768 {
769 if (
770 p->
buf[0] ==
'F' && p->
buf[1] ==
'F' && p->
buf[2] ==
'M' &&
771 (p->
buf[3] ==
'1' || p->
buf[3] ==
'2'))
773 return 0;
774 }
775
781 };
782
788 };
798 .priv_class = &ffm_class,
799 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int64_t avio_size(AVIOContext *s)
Get the filesize.
static const AVOption options[]
char * recommended_encoder_configuration
String containing paris of key and values describing recommended encoder configuration.
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int dct_algo
DCT algorithm, see FF_DCT_* below.
#define AV_OPT_FLAG_EXPORT
The option is inteded for exporting values to the caller.
float qblur
amount of qscale smoothing over time (0.0-1.0)
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
int64_t pos
byte position in stream, -1 if unknown
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
attribute_deprecated int frame_skip_cmp
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int ffm_append_recommended_configuration(AVStream *st, char **conf)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
unsigned int avio_rb16(AVIOContext *s)
static int ffm_resync(AVFormatContext *s, uint32_t state)
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
static int ffm_read_data(AVFormatContext *s, uint8_t *buf, int size, int header)
float i_quant_offset
qscale offset between P and I-frames
attribute_deprecated int me_method
This option does nothing.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
attribute_deprecated float rc_buffer_aggressivity
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
attribute_deprecated const char * rc_eq
static int64_t get_dts(AVFormatContext *s, int64_t pos)
#define FRAME_HEADER_SIZE
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
unsigned int avio_rb32(AVIOContext *s)
static av_cold int end(AVCodecContext *avctx)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
int me_cmp
motion estimation comparison function
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
static int ffm_is_avail_data(AVFormatContext *s, int size)
#define AVERROR_EOF
End of file.
static av_cold int read_close(AVFormatContext *ctx)
uint64_t avio_rb64(AVIOContext *s)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
static const uint8_t header[24]
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
static int ffm2_read_header(AVFormatContext *s)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void adjust_write_index(AVFormatContext *s)
unsigned int avio_rl32(AVIOContext *s)
int ffio_set_buf_size(AVIOContext *s, int buf_size)
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const AVClass ffm_class
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
static int ffm_probe(AVProbeData *p)
int flags
A combination of AV_PKT_FLAG values.
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
int avio_r8(AVIOContext *s)
AVCodecContext * codec
Codec context associated with this stream.
int rc_buffer_size
decoder bitstream buffer size
int intra_dc_precision
precision of the intra DC coefficient - 8
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
int64_t rc_min_rate
minimum bitrate
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int refs
number of reference frames
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
int width
picture width / height.
uint8_t header[FRAME_HEADER_SIZE+4]
int mb_decision
macroblock decision mode
Usually treated as AVMEDIA_TYPE_DATA.
int max_qdiff
maximum quantizer difference between frames
int buffer_size
Maximum buffer size.
attribute_deprecated int coder_type
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
static int read_header(FFV1Context *f)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
int frame_size
Number of samples per channel in an audio frame.
static int ffm_close(AVFormatContext *s)
enum AVMediaType codec_type
attribute_deprecated int mpeg_quant
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
attribute_deprecated int scenechange_threshold
char * av_strdup(const char *s)
Duplicate the string s.
int sample_rate
samples per second
AVIOContext * pb
I/O context.
attribute_deprecated int b_frame_strategy
main external API structure.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int qmin
minimum quantizer
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
static int write_index(NUTContext *nut, AVIOContext *bc)
Describe the class of an AVClass context structure.
static int ffm_read_header(AVFormatContext *s)
float b_quant_offset
qscale offset between IP and B-frames
This structure contains the data a format has to probe a file.
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
This struct describes the properties of a single codec described by an AVCodecID. ...
static int64_t pts
Global timestamp for the audio frames.
const AVClass * priv_class
AVClass for the private context.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
AVInputFormat ff_ffm_demuxer
static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
unsigned int avio_rl16(AVIOContext *s)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
uint8_t packet[FFM_PACKET_SIZE]
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
int64_t pos
position in the file of the current buffer
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
#define MKBETAG(a, b, c, d)
int eof_reached
true if eof reached
int channels
number of audio channels
void * priv_data
Format private data.
int flags2
AV_CODEC_FLAG2_*.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
#define MKTAG(a, b, c, d)
This structure stores compressed data.
int me_subpel_quality
subpel ME quality
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t rc_max_rate
maximum bitrate
int keyint_min
minimum GOP size