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