FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmdec.c
Go to the documentation of this file.
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/internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/intfloat.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "ffm.h"
33 #include "avio_internal.h"
34 
36 {
37  FFMContext *ffm = s->priv_data;
38  int64_t pos, avail_size;
39  int len;
40 
41  len = ffm->packet_end - ffm->packet_ptr;
42  if (size <= len)
43  return 1;
44  pos = avio_tell(s->pb);
45  if (!ffm->write_index) {
46  if (pos == ffm->file_size)
47  return AVERROR_EOF;
48  avail_size = ffm->file_size - pos;
49  } else {
50  if (pos == ffm->write_index) {
51  /* exactly at the end of stream */
52  return AVERROR(EAGAIN);
53  } else if (pos < ffm->write_index) {
54  avail_size = ffm->write_index - pos;
55  } else {
56  avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
57  }
58  }
59  avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
60  if (size <= avail_size)
61  return 1;
62  else
63  return AVERROR(EAGAIN);
64 }
65 
66 static int ffm_resync(AVFormatContext *s, int state)
67 {
68  av_log(s, AV_LOG_ERROR, "resyncing\n");
69  while (state != PACKET_ID) {
70  if (avio_feof(s->pb)) {
71  av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
72  return -1;
73  }
74  state = (state << 8) | avio_r8(s->pb);
75  }
76  return 0;
77 }
78 
79 /* first is true if we read the frame header */
81  uint8_t *buf, int size, int header)
82 {
83  FFMContext *ffm = s->priv_data;
84  AVIOContext *pb = s->pb;
85  int len, fill_size, size1, frame_offset, id;
86  int64_t last_pos = -1;
87 
88  size1 = size;
89  while (size > 0) {
90  redo:
91  len = ffm->packet_end - ffm->packet_ptr;
92  if (len < 0)
93  return -1;
94  if (len > size)
95  len = size;
96  if (len == 0) {
97  if (avio_tell(pb) == ffm->file_size)
98  avio_seek(pb, ffm->packet_size, SEEK_SET);
99  retry_read:
100  if (pb->buffer_size != ffm->packet_size) {
101  int64_t tell = avio_tell(pb);
102  int ret = ffio_set_buf_size(pb, ffm->packet_size);
103  if (ret < 0)
104  return ret;
105  avio_seek(pb, tell, SEEK_SET);
106  }
107  id = avio_rb16(pb); /* PACKET_ID */
108  if (id != PACKET_ID) {
109  if (ffm_resync(s, id) < 0)
110  return -1;
111  last_pos = avio_tell(pb);
112  }
113  fill_size = avio_rb16(pb);
114  ffm->dts = avio_rb64(pb);
115  frame_offset = avio_rb16(pb);
116  avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
117  ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
118  if (ffm->packet_end < ffm->packet || frame_offset < 0)
119  return -1;
120  /* if first packet or resynchronization packet, we must
121  handle it specifically */
122  if (ffm->first_packet || (frame_offset & 0x8000)) {
123  if (!frame_offset) {
124  /* This packet has no frame headers in it */
125  if (avio_tell(pb) >= ffm->packet_size * 3LL) {
126  int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
127  seekback = FFMAX(seekback, 0);
128  avio_seek(pb, -seekback, SEEK_CUR);
129  goto retry_read;
130  }
131  /* This is bad, we cannot find a valid frame header */
132  return 0;
133  }
134  ffm->first_packet = 0;
135  if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
136  return -1;
137  ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
138  if (!header)
139  break;
140  } else {
141  ffm->packet_ptr = ffm->packet;
142  }
143  goto redo;
144  }
145  memcpy(buf, ffm->packet_ptr, len);
146  buf += len;
147  ffm->packet_ptr += len;
148  size -= len;
149  header = 0;
150  }
151  return size1 - size;
152 }
153 
154 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
155  and file_size - FFM_PACKET_SIZE */
156 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
157 {
158  FFMContext *ffm = s->priv_data;
159  AVIOContext *pb = s->pb;
160  int64_t pos;
161 
162  pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
163  pos = FFMAX(pos, FFM_PACKET_SIZE);
164  ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
165  return avio_seek(pb, pos, SEEK_SET);
166 }
167 
168 static int64_t get_dts(AVFormatContext *s, int64_t pos)
169 {
170  AVIOContext *pb = s->pb;
171  int64_t dts;
172 
173  ffm_seek1(s, pos);
174  avio_skip(pb, 4);
175  dts = avio_rb64(pb);
176  ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
177  return dts;
178 }
179 
181 {
182  FFMContext *ffm = s->priv_data;
183  AVIOContext *pb = s->pb;
184  int64_t pts;
185  //int64_t orig_write_index = ffm->write_index;
186  int64_t pos_min, pos_max;
187  int64_t pts_start;
188  int64_t ptr = avio_tell(pb);
189 
190 
191  pos_min = 0;
192  pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
193 
194  pts_start = get_dts(s, pos_min);
195 
196  pts = get_dts(s, pos_max);
197 
198  if (pts - 100000 > pts_start)
199  goto end;
200 
202 
203  pts_start = get_dts(s, pos_min);
204 
205  pts = get_dts(s, pos_max);
206 
207  if (pts - 100000 <= pts_start) {
208  while (1) {
209  int64_t newpos;
210  int64_t newpts;
211 
212  newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
213 
214  if (newpos == pos_min)
215  break;
216 
217  newpts = get_dts(s, newpos);
218 
219  if (newpts - 100000 <= pts) {
220  pos_max = newpos;
221  pts = newpts;
222  } else {
223  pos_min = newpos;
224  }
225  }
226  ffm->write_index += pos_max;
227  }
228 
229  end:
230  avio_seek(pb, ptr, SEEK_SET);
231 }
232 
233 
235 {
236  int i;
237 
238  for (i = 0; i < s->nb_streams; i++)
239  av_freep(&s->streams[i]->codec->rc_eq);
240 
241  return 0;
242 }
243 
244 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
245 {
246  int ret;
247  size_t newsize;
248  av_assert0(conf && st);
249  if (!*conf)
250  return 0;
253  *conf = 0;
254  return 0;
255  }
256  newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
257  if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
258  return ret;
260  av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
261  av_freep(conf);
262  return 0;
263 }
264 
266 {
267  FFMContext *ffm = s->priv_data;
268  AVStream *st;
269  AVIOContext *pb = s->pb;
270  AVCodecContext *codec;
271  int ret;
272  int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
273  AVCodec *enc;
274  char *buffer;
275 
276  ffm->packet_size = avio_rb32(pb);
277  if (ffm->packet_size != FFM_PACKET_SIZE) {
278  av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
280  ret = AVERROR_INVALIDDATA;
281  goto fail;
282  }
283 
284  ffm->write_index = avio_rb64(pb);
285  /* get also filesize */
286  if (pb->seekable) {
287  ffm->file_size = avio_size(pb);
288  if (ffm->write_index && 0)
290  } else {
291  ffm->file_size = (UINT64_C(1) << 63) - 1;
292  }
293 
294  while(!avio_feof(pb)) {
295  unsigned id = avio_rb32(pb);
296  unsigned size = avio_rb32(pb);
297  int64_t next = avio_tell(pb) + size;
298  char rc_eq_buf[128];
299 
300  if(!id)
301  break;
302 
303  switch(id) {
304  case MKBETAG('M', 'A', 'I', 'N'):
305  if (f_main++) {
306  ret = AVERROR(EINVAL);
307  goto fail;
308  }
309  avio_rb32(pb); /* nb_streams */
310  avio_rb32(pb); /* total bitrate */
311  break;
312  case MKBETAG('C', 'O', 'M', 'M'):
313  f_cprv = f_stvi = f_stau = 0;
314  st = avformat_new_stream(s, NULL);
315  if (!st) {
316  ret = AVERROR(ENOMEM);
317  goto fail;
318  }
319 
320  avpriv_set_pts_info(st, 64, 1, 1000000);
321 
322  codec = st->codec;
323  /* generic info */
324  codec->codec_id = avio_rb32(pb);
325  codec->codec_type = avio_r8(pb);
326  codec->bit_rate = avio_rb32(pb);
327  codec->flags = avio_rb32(pb);
328  codec->flags2 = avio_rb32(pb);
329  codec->debug = avio_rb32(pb);
330  if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
331  if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
332  return AVERROR(ENOMEM);
333  }
334  break;
335  case MKBETAG('S', 'T', 'V', 'I'):
336  if (f_stvi++) {
337  ret = AVERROR(EINVAL);
338  goto fail;
339  }
340  codec->time_base.num = avio_rb32(pb);
341  codec->time_base.den = avio_rb32(pb);
342  if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
343  av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
344  codec->time_base.num, codec->time_base.den);
345  ret = AVERROR_INVALIDDATA;
346  goto fail;
347  }
348  codec->width = avio_rb16(pb);
349  codec->height = avio_rb16(pb);
350  codec->gop_size = avio_rb16(pb);
351  codec->pix_fmt = avio_rb32(pb);
352  codec->qmin = avio_r8(pb);
353  codec->qmax = avio_r8(pb);
354  codec->max_qdiff = avio_r8(pb);
355  codec->qcompress = avio_rb16(pb) / 10000.0;
356  codec->qblur = avio_rb16(pb) / 10000.0;
357  codec->bit_rate_tolerance = avio_rb32(pb);
358  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
359  codec->rc_eq = av_strdup(rc_eq_buf);
360  codec->rc_max_rate = avio_rb32(pb);
361  codec->rc_min_rate = avio_rb32(pb);
362  codec->rc_buffer_size = avio_rb32(pb);
363  codec->i_quant_factor = av_int2double(avio_rb64(pb));
364  codec->b_quant_factor = av_int2double(avio_rb64(pb));
365  codec->i_quant_offset = av_int2double(avio_rb64(pb));
366  codec->b_quant_offset = av_int2double(avio_rb64(pb));
367  codec->dct_algo = avio_rb32(pb);
368  codec->strict_std_compliance = avio_rb32(pb);
369  codec->max_b_frames = avio_rb32(pb);
370  codec->mpeg_quant = avio_rb32(pb);
371  codec->intra_dc_precision = avio_rb32(pb);
372  codec->me_method = avio_rb32(pb);
373  codec->mb_decision = avio_rb32(pb);
374  codec->nsse_weight = avio_rb32(pb);
375  codec->frame_skip_cmp = avio_rb32(pb);
377  codec->codec_tag = avio_rb32(pb);
378  codec->thread_count = avio_r8(pb);
379  codec->coder_type = avio_rb32(pb);
380  codec->me_cmp = avio_rb32(pb);
381  codec->me_subpel_quality = avio_rb32(pb);
382  codec->me_range = avio_rb32(pb);
383  codec->keyint_min = avio_rb32(pb);
384  codec->scenechange_threshold = avio_rb32(pb);
385  codec->b_frame_strategy = avio_rb32(pb);
386  codec->qcompress = av_int2double(avio_rb64(pb));
387  codec->qblur = av_int2double(avio_rb64(pb));
388  codec->max_qdiff = avio_rb32(pb);
389  codec->refs = avio_rb32(pb);
390  break;
391  case MKBETAG('S', 'T', 'A', 'U'):
392  if (f_stau++) {
393  ret = AVERROR(EINVAL);
394  goto fail;
395  }
396  codec->sample_rate = avio_rb32(pb);
397  codec->channels = avio_rl16(pb);
398  codec->frame_size = avio_rl16(pb);
399  break;
400  case MKBETAG('C', 'P', 'R', 'V'):
401  if (f_cprv++) {
402  ret = AVERROR(EINVAL);
403  goto fail;
404  }
405  enc = avcodec_find_encoder(codec->codec_id);
406  if (enc && enc->priv_data_size && enc->priv_class) {
407  buffer = av_malloc(size + 1);
408  if (!buffer) {
409  ret = AVERROR(ENOMEM);
410  goto fail;
411  }
412  avio_get_str(pb, size, buffer, size + 1);
413  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
414  goto fail;
415  }
416  break;
417  case MKBETAG('S', '2', 'V', 'I'):
418  if (f_stvi++) {
419  ret = AVERROR(EINVAL);
420  goto fail;
421  }
422  buffer = av_malloc(size);
423  if (!buffer) {
424  ret = AVERROR(ENOMEM);
425  goto fail;
426  }
427  avio_get_str(pb, INT_MAX, buffer, size);
428  av_set_options_string(codec, buffer, "=", ",");
429  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
430  goto fail;
431  break;
432  case MKBETAG('S', '2', 'A', 'U'):
433  if (f_stau++) {
434  ret = AVERROR(EINVAL);
435  goto fail;
436  }
437  buffer = av_malloc(size);
438  if (!buffer) {
439  ret = AVERROR(ENOMEM);
440  goto fail;
441  }
442  avio_get_str(pb, INT_MAX, buffer, size);
443  av_set_options_string(codec, buffer, "=", ",");
444  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
445  goto fail;
446  break;
447  }
448  avio_seek(pb, next, SEEK_SET);
449  }
450 
451  /* get until end of block reached */
452  while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
453  avio_r8(pb);
454 
455  /* init packet demux */
456  ffm->packet_ptr = ffm->packet;
457  ffm->packet_end = ffm->packet;
458  ffm->frame_offset = 0;
459  ffm->dts = 0;
460  ffm->read_state = READ_HEADER;
461  ffm->first_packet = 1;
462  return 0;
463  fail:
464  ffm_close(s);
465  return ret;
466 }
467 
469 {
470  FFMContext *ffm = s->priv_data;
471  AVStream *st;
472  AVIOContext *pb = s->pb;
473  AVCodecContext *codec;
474  int i, nb_streams;
475  uint32_t tag;
476 
477  /* header */
478  tag = avio_rl32(pb);
479  if (tag == MKTAG('F', 'F', 'M', '2'))
480  return ffm2_read_header(s);
481  if (tag != MKTAG('F', 'F', 'M', '1'))
482  goto fail;
483  ffm->packet_size = avio_rb32(pb);
484  if (ffm->packet_size != FFM_PACKET_SIZE)
485  goto fail;
486  ffm->write_index = avio_rb64(pb);
487  /* get also filesize */
488  if (pb->seekable) {
489  ffm->file_size = avio_size(pb);
490  if (ffm->write_index && 0)
492  } else {
493  ffm->file_size = (UINT64_C(1) << 63) - 1;
494  }
495 
496  nb_streams = avio_rb32(pb);
497  avio_rb32(pb); /* total bitrate */
498  /* read each stream */
499  for(i=0;i<nb_streams;i++) {
500  char rc_eq_buf[128];
501 
502  st = avformat_new_stream(s, NULL);
503  if (!st)
504  goto fail;
505 
506  avpriv_set_pts_info(st, 64, 1, 1000000);
507 
508  codec = st->codec;
509  /* generic info */
510  codec->codec_id = avio_rb32(pb);
511  codec->codec_type = avio_r8(pb); /* codec_type */
512  codec->bit_rate = avio_rb32(pb);
513  codec->flags = avio_rb32(pb);
514  codec->flags2 = avio_rb32(pb);
515  codec->debug = avio_rb32(pb);
516  /* specific info */
517  switch(codec->codec_type) {
518  case AVMEDIA_TYPE_VIDEO:
519  codec->time_base.num = avio_rb32(pb);
520  codec->time_base.den = avio_rb32(pb);
521  if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
522  av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
523  codec->time_base.num, codec->time_base.den);
524  goto fail;
525  }
526  codec->width = avio_rb16(pb);
527  codec->height = avio_rb16(pb);
528  codec->gop_size = avio_rb16(pb);
529  codec->pix_fmt = avio_rb32(pb);
530  codec->qmin = avio_r8(pb);
531  codec->qmax = avio_r8(pb);
532  codec->max_qdiff = avio_r8(pb);
533  codec->qcompress = avio_rb16(pb) / 10000.0;
534  codec->qblur = avio_rb16(pb) / 10000.0;
535  codec->bit_rate_tolerance = avio_rb32(pb);
536  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
537  codec->rc_eq = av_strdup(rc_eq_buf);
538  codec->rc_max_rate = avio_rb32(pb);
539  codec->rc_min_rate = avio_rb32(pb);
540  codec->rc_buffer_size = avio_rb32(pb);
541  codec->i_quant_factor = av_int2double(avio_rb64(pb));
542  codec->b_quant_factor = av_int2double(avio_rb64(pb));
543  codec->i_quant_offset = av_int2double(avio_rb64(pb));
544  codec->b_quant_offset = av_int2double(avio_rb64(pb));
545  codec->dct_algo = avio_rb32(pb);
546  codec->strict_std_compliance = avio_rb32(pb);
547  codec->max_b_frames = avio_rb32(pb);
548  codec->mpeg_quant = avio_rb32(pb);
549  codec->intra_dc_precision = avio_rb32(pb);
550  codec->me_method = avio_rb32(pb);
551  codec->mb_decision = avio_rb32(pb);
552  codec->nsse_weight = avio_rb32(pb);
553  codec->frame_skip_cmp = avio_rb32(pb);
555  codec->codec_tag = avio_rb32(pb);
556  codec->thread_count = avio_r8(pb);
557  codec->coder_type = avio_rb32(pb);
558  codec->me_cmp = avio_rb32(pb);
559  codec->me_subpel_quality = avio_rb32(pb);
560  codec->me_range = avio_rb32(pb);
561  codec->keyint_min = avio_rb32(pb);
562  codec->scenechange_threshold = avio_rb32(pb);
563  codec->b_frame_strategy = avio_rb32(pb);
564  codec->qcompress = av_int2double(avio_rb64(pb));
565  codec->qblur = av_int2double(avio_rb64(pb));
566  codec->max_qdiff = avio_rb32(pb);
567  codec->refs = avio_rb32(pb);
568  break;
569  case AVMEDIA_TYPE_AUDIO:
570  codec->sample_rate = avio_rb32(pb);
571  codec->channels = avio_rl16(pb);
572  codec->frame_size = avio_rl16(pb);
573  break;
574  default:
575  goto fail;
576  }
577  if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
578  if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
579  return AVERROR(ENOMEM);
580  }
581  }
582 
583  /* get until end of block reached */
584  while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
585  avio_r8(pb);
586 
587  /* init packet demux */
588  ffm->packet_ptr = ffm->packet;
589  ffm->packet_end = ffm->packet;
590  ffm->frame_offset = 0;
591  ffm->dts = 0;
592  ffm->read_state = READ_HEADER;
593  ffm->first_packet = 1;
594  return 0;
595  fail:
596  ffm_close(s);
597  return -1;
598 }
599 
600 /* return < 0 if eof */
602 {
603  int size;
604  FFMContext *ffm = s->priv_data;
605  int duration, ret;
606 
607  switch(ffm->read_state) {
608  case READ_HEADER:
609  if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
610  return ret;
611 
612  ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
613  avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
614  if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
616  return -1;
617  if (ffm->header[1] & FLAG_DTS)
618  if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
619  return -1;
620  ffm->read_state = READ_DATA;
621  /* fall through */
622  case READ_DATA:
623  size = AV_RB24(ffm->header + 2);
624  if ((ret = ffm_is_avail_data(s, size)) < 0)
625  return ret;
626 
627  duration = AV_RB24(ffm->header + 5);
628 
629  if (av_new_packet(pkt, size) < 0) {
630  return AVERROR(ENOMEM);
631  }
632  pkt->stream_index = ffm->header[0];
633  if ((unsigned)pkt->stream_index >= s->nb_streams) {
634  av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
635  av_free_packet(pkt);
636  ffm->read_state = READ_HEADER;
637  return -1;
638  }
639  pkt->pos = avio_tell(s->pb);
640  if (ffm->header[1] & FLAG_KEY_FRAME)
641  pkt->flags |= AV_PKT_FLAG_KEY;
642 
643  ffm->read_state = READ_HEADER;
644  if (ffm_read_data(s, pkt->data, size, 0) != size) {
645  /* bad case: desynchronized packet. we cancel all the packet loading */
646  av_free_packet(pkt);
647  return -1;
648  }
649  pkt->pts = AV_RB64(ffm->header+8);
650  if (ffm->header[1] & FLAG_DTS)
651  pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
652  else
653  pkt->dts = pkt->pts;
654  pkt->duration = duration;
655  break;
656  }
657  return 0;
658 }
659 
660 /* seek to a given time in the file. The file read pointer is
661  positioned at or before pts. XXX: the following code is quite
662  approximative */
663 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
664 {
665  FFMContext *ffm = s->priv_data;
666  int64_t pos_min, pos_max, pos;
667  int64_t pts_min, pts_max, pts;
668  double pos1;
669 
670  ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
671  /* find the position using linear interpolation (better than
672  dichotomy in typical cases) */
673  if (ffm->write_index && ffm->write_index < ffm->file_size) {
674  if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
675  pos_min = FFM_PACKET_SIZE;
676  pos_max = ffm->write_index - FFM_PACKET_SIZE;
677  } else {
678  pos_min = ffm->write_index;
679  pos_max = ffm->file_size - FFM_PACKET_SIZE;
680  }
681  } else {
682  pos_min = FFM_PACKET_SIZE;
683  pos_max = ffm->file_size - FFM_PACKET_SIZE;
684  }
685  while (pos_min <= pos_max) {
686  pts_min = get_dts(s, pos_min);
687  pts_max = get_dts(s, pos_max);
688  if (pts_min > wanted_pts || pts_max <= wanted_pts) {
689  pos = pts_min > wanted_pts ? pos_min : pos_max;
690  goto found;
691  }
692  /* linear interpolation */
693  pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
694  (double)(pts_max - pts_min);
695  pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
696  if (pos <= pos_min)
697  pos = pos_min;
698  else if (pos >= pos_max)
699  pos = pos_max;
700  pts = get_dts(s, pos);
701  /* check if we are lucky */
702  if (pts == wanted_pts) {
703  goto found;
704  } else if (pts > wanted_pts) {
705  pos_max = pos - FFM_PACKET_SIZE;
706  } else {
707  pos_min = pos + FFM_PACKET_SIZE;
708  }
709  }
710  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
711 
712  found:
713  if (ffm_seek1(s, pos) < 0)
714  return -1;
715 
716  /* reset read state */
717  ffm->read_state = READ_HEADER;
718  ffm->packet_ptr = ffm->packet;
719  ffm->packet_end = ffm->packet;
720  ffm->first_packet = 1;
721 
722  return 0;
723 }
724 
725 static int ffm_probe(AVProbeData *p)
726 {
727  if (
728  p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
729  (p->buf[3] == '1' || p->buf[3] == '2'))
730  return AVPROBE_SCORE_MAX + 1;
731  return 0;
732 }
733 
735  .name = "ffm",
736  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
737  .priv_data_size = sizeof(FFMContext),
742  .read_seek = ffm_seek,
743 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2277
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define PACKET_ID
Definition: ffm.h:32
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:282
int64_t dts
Definition: ffm.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
char * recommended_encoder_configuration
String containing paris of key and values describing recommended encoder configuration.
Definition: avformat.h:1173
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2997
int mpeg_quant
0-> h263 quant 1-> mpeg quant
Definition: avcodec.h:1819
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2948
enum AVCodecID id
Definition: mxfenc.c:101
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:2540
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1448
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4083
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2945
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...
Definition: avcodec.h:1780
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
int num
numerator
Definition: rational.h:44
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.
Definition: opt.c:1300
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
static int ffm_append_recommended_configuration(AVStream *st, char **conf)
Definition: ffmdec.c:244
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
static AVPacket pkt
#define FLAG_DTS
Definition: ffm.h:37
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:675
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2706
AVCodec.
Definition: avcodec.h:3472
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmdec.c:601
static int ffm_read_data(AVFormatContext *s, uint8_t *buf, int size, int header)
Definition: ffmdec.c:80
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1835
attribute_deprecated int me_method
This option does nothing.
Definition: avcodec.h:1729
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:2079
#define FFM_HEADER_SIZE
Definition: ffm.h:30
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1631
static int ffm_resync(AVFormatContext *s, int state)
Definition: ffmdec.c:66
Format I/O context.
Definition: avformat.h:1273
Definition: ffm.h:41
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int64_t file_size
Definition: ffm.h:46
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1575
attribute_deprecated float rc_buffer_aggressivity
Definition: avcodec.h:2618
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
attribute_deprecated const char * rc_eq
Definition: avcodec.h:2596
uint8_t
static int nb_streams
Definition: ffprobe.c:226
#define av_malloc(s)
static int64_t get_dts(AVFormatContext *s, int64_t pos)
Definition: ffmdec.c:168
AVOptions.
#define FRAME_HEADER_SIZE
Definition: cpia.c:30
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:2014
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:690
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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...
Definition: avcodec.h:1789
uint8_t * packet_end
Definition: ffm.h:55
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1909
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
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
static int ffm_is_avail_data(AVFormatContext *s, int size)
Definition: ffmdec.c:35
int coder_type
coder type
Definition: avcodec.h:2657
uint8_t * data
Definition: avcodec.h:1423
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
uint32_t tag
Definition: movenc.c:1334
#define ff_dlog(a,...)
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:757
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
static const uint8_t header[24]
Definition: sdr2.c:67
static int64_t duration
Definition: ffplay.c:326
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:538
static int ffm2_read_header(AVFormatContext *s)
Definition: ffmdec.c:265
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
uint8_t * packet_ptr
Definition: ffm.h:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void adjust_write_index(AVFormatContext *s)
Definition: ffmdec.c:180
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:659
#define AVERROR(e)
Definition: error.h:43
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:839
int qmax
maximum quantizer
Definition: avcodec.h:2554
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1597
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2604
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*...
Definition: avcodec.h:1828
static int ffm_probe(AVProbeData *p)
Definition: ffmdec.c:725
#define FFMAX(a, b)
Definition: common.h:79
#define fail()
Definition: checkasm.h:57
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:529
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2581
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:2107
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
int refs
number of reference frames
Definition: avcodec.h:2178
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int bit_rate
the average bitrate
Definition: avcodec.h:1567
#define FFMIN(a, b)
Definition: common.h:81
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1681
int priv_data_size
Definition: avcodec.h:3508
static struct @197 state
int b_frame_strategy
Definition: avcodec.h:1797
uint8_t header[FRAME_HEADER_SIZE+4]
Definition: ffm.h:48
int mb_decision
macroblock decision mode
Definition: avcodec.h:2054
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2561
int packet_size
Definition: ffm.h:52
int buffer_size
Maximum buffer size.
Definition: avio.h:126
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3033
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2282
static int ffm_close(AVFormatContext *s)
Definition: ffmdec.c:234
enum AVMediaType codec_type
Definition: avcodec.h:1510
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
Definition: bytestream.h:87
enum AVCodecID codec_id
Definition: avcodec.h:1519
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int sample_rate
samples per second
Definition: avcodec.h:2262
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
int debug
debug
Definition: avcodec.h:2842
Definition: ffm.h:44
main external API structure.
Definition: avcodec.h:1502
int qmin
minimum quantizer
Definition: avcodec.h:2547
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:578
void * buf
Definition: avisynth_c.h:553
static int ffm_read_header(AVFormatContext *s)
Definition: ffmdec.c:468
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1804
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2539
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...
Definition: avstring.c:93
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3498
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:784
AVInputFormat ff_ffm_demuxer
Definition: ffmdec.c:734
static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
Definition: ffmdec.c:156
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1707
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:643
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
Definition: bytestream.h:87
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
Definition: avcodec.h:3108
int64_t pos
position in the file of the current buffer
Definition: avio.h:137
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
Definition: ffmdec.c:663
int den
denominator
Definition: rational.h:45
#define MKBETAG(a, b, c, d)
Definition: common.h:331
int eof_reached
true if eof reached
Definition: avio.h:139
int len
int channels
number of audio channels
Definition: avcodec.h:2263
void * priv_data
Format private data.
Definition: avformat.h:1301
int read_state
Definition: ffm.h:47
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1604
int64_t write_index
Definition: ffm.h:46
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:715
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:301
int stream_index
Definition: avcodec.h:1425
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2611
#define MKTAG(a, b, c, d)
Definition: common.h:330
This structure stores compressed data.
Definition: avcodec.h:1400
int me_subpel_quality
subpel ME quality
Definition: avcodec.h:1985
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2820
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
GLuint buffer
Definition: opengl_enc.c:102
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int keyint_min
minimum GOP size
Definition: avcodec.h:2171