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 "libavutil/intreadwrite.h"
23 #include "libavutil/intfloat.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "ffm.h"
27 #include "avio_internal.h"
28 
30 {
31  FFMContext *ffm = s->priv_data;
32  int64_t pos, avail_size;
33  int len;
34 
35  len = ffm->packet_end - ffm->packet_ptr;
36  if (size <= len)
37  return 1;
38  pos = avio_tell(s->pb);
39  if (!ffm->write_index) {
40  if (pos == ffm->file_size)
41  return AVERROR_EOF;
42  avail_size = ffm->file_size - pos;
43  } else {
44  if (pos == ffm->write_index) {
45  /* exactly at the end of stream */
46  return AVERROR(EAGAIN);
47  } else if (pos < ffm->write_index) {
48  avail_size = ffm->write_index - pos;
49  } else {
50  avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
51  }
52  }
53  avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
54  if (size <= avail_size)
55  return 1;
56  else
57  return AVERROR(EAGAIN);
58 }
59 
60 static int ffm_resync(AVFormatContext *s, int state)
61 {
62  av_log(s, AV_LOG_ERROR, "resyncing\n");
63  while (state != PACKET_ID) {
64  if (url_feof(s->pb)) {
65  av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
66  return -1;
67  }
68  state = (state << 8) | avio_r8(s->pb);
69  }
70  return 0;
71 }
72 
73 /* first is true if we read the frame header */
75  uint8_t *buf, int size, int header)
76 {
77  FFMContext *ffm = s->priv_data;
78  AVIOContext *pb = s->pb;
79  int len, fill_size, size1, frame_offset, id;
80 
81  size1 = size;
82  while (size > 0) {
83  redo:
84  len = ffm->packet_end - ffm->packet_ptr;
85  if (len < 0)
86  return -1;
87  if (len > size)
88  len = size;
89  if (len == 0) {
90  if (avio_tell(pb) == ffm->file_size)
91  avio_seek(pb, ffm->packet_size, SEEK_SET);
92  retry_read:
93  if (pb->buffer_size != ffm->packet_size) {
94  int64_t tell = avio_tell(pb);
96  avio_seek(pb, tell, SEEK_SET);
97  }
98  id = avio_rb16(pb); /* PACKET_ID */
99  if (id != PACKET_ID)
100  if (ffm_resync(s, id) < 0)
101  return -1;
102  fill_size = avio_rb16(pb);
103  ffm->dts = avio_rb64(pb);
104  frame_offset = avio_rb16(pb);
105  avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
106  ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
107  if (ffm->packet_end < ffm->packet || frame_offset < 0)
108  return -1;
109  /* if first packet or resynchronization packet, we must
110  handle it specifically */
111  if (ffm->first_packet || (frame_offset & 0x8000)) {
112  if (!frame_offset) {
113  /* This packet has no frame headers in it */
114  if (avio_tell(pb) >= ffm->packet_size * 3LL) {
115  avio_seek(pb, -ffm->packet_size * 2LL, SEEK_CUR);
116  goto retry_read;
117  }
118  /* This is bad, we cannot find a valid frame header */
119  return 0;
120  }
121  ffm->first_packet = 0;
122  if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
123  return -1;
124  ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
125  if (!header)
126  break;
127  } else {
128  ffm->packet_ptr = ffm->packet;
129  }
130  goto redo;
131  }
132  memcpy(buf, ffm->packet_ptr, len);
133  buf += len;
134  ffm->packet_ptr += len;
135  size -= len;
136  header = 0;
137  }
138  return size1 - size;
139 }
140 
141 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
142  and file_size - FFM_PACKET_SIZE */
143 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
144 {
145  FFMContext *ffm = s->priv_data;
146  AVIOContext *pb = s->pb;
147  int64_t pos;
148 
149  pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
150  pos = FFMAX(pos, FFM_PACKET_SIZE);
151  av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
152  return avio_seek(pb, pos, SEEK_SET);
153 }
154 
155 static int64_t get_dts(AVFormatContext *s, int64_t pos)
156 {
157  AVIOContext *pb = s->pb;
158  int64_t dts;
159 
160  ffm_seek1(s, pos);
161  avio_skip(pb, 4);
162  dts = avio_rb64(pb);
163  av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
164  return dts;
165 }
166 
168 {
169  FFMContext *ffm = s->priv_data;
170  AVIOContext *pb = s->pb;
171  int64_t pts;
172  //int64_t orig_write_index = ffm->write_index;
173  int64_t pos_min, pos_max;
174  int64_t pts_start;
175  int64_t ptr = avio_tell(pb);
176 
177 
178  pos_min = 0;
179  pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
180 
181  pts_start = get_dts(s, pos_min);
182 
183  pts = get_dts(s, pos_max);
184 
185  if (pts - 100000 > pts_start)
186  goto end;
187 
189 
190  pts_start = get_dts(s, pos_min);
191 
192  pts = get_dts(s, pos_max);
193 
194  if (pts - 100000 <= pts_start) {
195  while (1) {
196  int64_t newpos;
197  int64_t newpts;
198 
199  newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
200 
201  if (newpos == pos_min)
202  break;
203 
204  newpts = get_dts(s, newpos);
205 
206  if (newpts - 100000 <= pts) {
207  pos_max = newpos;
208  pts = newpts;
209  } else {
210  pos_min = newpos;
211  }
212  }
213  ffm->write_index += pos_max;
214  }
215 
216  end:
217  avio_seek(pb, ptr, SEEK_SET);
218 }
219 
220 
222 {
223  int i;
224 
225  for (i = 0; i < s->nb_streams; i++)
226  av_freep(&s->streams[i]->codec->rc_eq);
227 
228  return 0;
229 }
230 
232 {
233  FFMContext *ffm = s->priv_data;
234  AVStream *st;
235  AVIOContext *pb = s->pb;
236  AVCodecContext *codec;
237 
238  ffm->packet_size = avio_rb32(pb);
239  if (ffm->packet_size != FFM_PACKET_SIZE)
240  goto fail;
241  ffm->write_index = avio_rb64(pb);
242  /* get also filesize */
243  if (pb->seekable) {
244  ffm->file_size = avio_size(pb);
245  if (ffm->write_index && 0)
247  } else {
248  ffm->file_size = (UINT64_C(1) << 63) - 1;
249  }
250 
251  while(!url_feof(pb)) {
252  unsigned id = avio_rb32(pb);
253  unsigned size = avio_rb32(pb);
254  int64_t next = avio_tell(pb) + size;
255  char rc_eq_buf[128];
256 
257  if(!id)
258  break;
259 
260  switch(id) {
261  case MKBETAG('M', 'A', 'I', 'N'):
262  avio_rb32(pb); /* nb_streams */
263  avio_rb32(pb); /* total bitrate */
264  break;
265  case MKBETAG('C', 'O', 'M', 'M'):
266  st = avformat_new_stream(s, NULL);
267  if (!st)
268  goto fail;
269 
270  avpriv_set_pts_info(st, 64, 1, 1000000);
271 
272  codec = st->codec;
273  /* generic info */
274  codec->codec_id = avio_rb32(pb);
275  codec->codec_type = avio_r8(pb);
276  codec->bit_rate = avio_rb32(pb);
277  codec->flags = avio_rb32(pb);
278  codec->flags2 = avio_rb32(pb);
279  codec->debug = avio_rb32(pb);
280  if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
281  if (ff_alloc_extradata(codec, avio_rb32(pb)))
282  return AVERROR(ENOMEM);
283  avio_read(pb, codec->extradata, codec->extradata_size);
284  }
285  avio_seek(pb, next, SEEK_SET);
286  id = avio_rb32(pb);
287  size = avio_rb32(pb);
288  next = avio_tell(pb) + size;
289  switch(id) {
290  case MKBETAG('S', 'T', 'V', 'I'):
291  codec->time_base.num = avio_rb32(pb);
292  codec->time_base.den = avio_rb32(pb);
293  codec->width = avio_rb16(pb);
294  codec->height = avio_rb16(pb);
295  codec->gop_size = avio_rb16(pb);
296  codec->pix_fmt = avio_rb32(pb);
297  codec->qmin = avio_r8(pb);
298  codec->qmax = avio_r8(pb);
299  codec->max_qdiff = avio_r8(pb);
300  codec->qcompress = avio_rb16(pb) / 10000.0;
301  codec->qblur = avio_rb16(pb) / 10000.0;
302  codec->bit_rate_tolerance = avio_rb32(pb);
303  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
304  codec->rc_eq = av_strdup(rc_eq_buf);
305  codec->rc_max_rate = avio_rb32(pb);
306  codec->rc_min_rate = avio_rb32(pb);
307  codec->rc_buffer_size = avio_rb32(pb);
308  codec->i_quant_factor = av_int2double(avio_rb64(pb));
309  codec->b_quant_factor = av_int2double(avio_rb64(pb));
310  codec->i_quant_offset = av_int2double(avio_rb64(pb));
311  codec->b_quant_offset = av_int2double(avio_rb64(pb));
312  codec->dct_algo = avio_rb32(pb);
313  codec->strict_std_compliance = avio_rb32(pb);
314  codec->max_b_frames = avio_rb32(pb);
315  codec->mpeg_quant = avio_rb32(pb);
316  codec->intra_dc_precision = avio_rb32(pb);
317  codec->me_method = avio_rb32(pb);
318  codec->mb_decision = avio_rb32(pb);
319  codec->nsse_weight = avio_rb32(pb);
320  codec->frame_skip_cmp = avio_rb32(pb);
322  codec->codec_tag = avio_rb32(pb);
323  codec->thread_count = avio_r8(pb);
324  codec->coder_type = avio_rb32(pb);
325  codec->me_cmp = avio_rb32(pb);
326  codec->me_subpel_quality = avio_rb32(pb);
327  codec->me_range = avio_rb32(pb);
328  codec->keyint_min = avio_rb32(pb);
329  codec->scenechange_threshold = avio_rb32(pb);
330  codec->b_frame_strategy = avio_rb32(pb);
331  codec->qcompress = av_int2double(avio_rb64(pb));
332  codec->qblur = av_int2double(avio_rb64(pb));
333  codec->max_qdiff = avio_rb32(pb);
334  codec->refs = avio_rb32(pb);
335  break;
336  case MKBETAG('S', 'T', 'A', 'U'):
337  codec->sample_rate = avio_rb32(pb);
338  codec->channels = avio_rl16(pb);
339  codec->frame_size = avio_rl16(pb);
340  break;
341  }
342  break;
343  }
344  avio_seek(pb, next, SEEK_SET);
345  }
346 
347  /* get until end of block reached */
348  while ((avio_tell(pb) % ffm->packet_size) != 0)
349  avio_r8(pb);
350 
351  /* init packet demux */
352  ffm->packet_ptr = ffm->packet;
353  ffm->packet_end = ffm->packet;
354  ffm->frame_offset = 0;
355  ffm->dts = 0;
356  ffm->read_state = READ_HEADER;
357  ffm->first_packet = 1;
358  return 0;
359  fail:
360  ffm_close(s);
361  return -1;
362 }
363 
365 {
366  FFMContext *ffm = s->priv_data;
367  AVStream *st;
368  AVIOContext *pb = s->pb;
369  AVCodecContext *codec;
370  int i, nb_streams;
371  uint32_t tag;
372 
373  /* header */
374  tag = avio_rl32(pb);
375  if (tag == MKTAG('F', 'F', 'M', '2'))
376  return ffm2_read_header(s);
377  if (tag != MKTAG('F', 'F', 'M', '1'))
378  goto fail;
379  ffm->packet_size = avio_rb32(pb);
380  if (ffm->packet_size != FFM_PACKET_SIZE)
381  goto fail;
382  ffm->write_index = avio_rb64(pb);
383  /* get also filesize */
384  if (pb->seekable) {
385  ffm->file_size = avio_size(pb);
386  if (ffm->write_index && 0)
388  } else {
389  ffm->file_size = (UINT64_C(1) << 63) - 1;
390  }
391 
392  nb_streams = avio_rb32(pb);
393  avio_rb32(pb); /* total bitrate */
394  /* read each stream */
395  for(i=0;i<nb_streams;i++) {
396  char rc_eq_buf[128];
397 
398  st = avformat_new_stream(s, NULL);
399  if (!st)
400  goto fail;
401 
402  avpriv_set_pts_info(st, 64, 1, 1000000);
403 
404  codec = st->codec;
405  /* generic info */
406  codec->codec_id = avio_rb32(pb);
407  codec->codec_type = avio_r8(pb); /* codec_type */
408  codec->bit_rate = avio_rb32(pb);
409  codec->flags = avio_rb32(pb);
410  codec->flags2 = avio_rb32(pb);
411  codec->debug = avio_rb32(pb);
412  /* specific info */
413  switch(codec->codec_type) {
414  case AVMEDIA_TYPE_VIDEO:
415  codec->time_base.num = avio_rb32(pb);
416  codec->time_base.den = avio_rb32(pb);
417  codec->width = avio_rb16(pb);
418  codec->height = avio_rb16(pb);
419  codec->gop_size = avio_rb16(pb);
420  codec->pix_fmt = avio_rb32(pb);
421  codec->qmin = avio_r8(pb);
422  codec->qmax = avio_r8(pb);
423  codec->max_qdiff = avio_r8(pb);
424  codec->qcompress = avio_rb16(pb) / 10000.0;
425  codec->qblur = avio_rb16(pb) / 10000.0;
426  codec->bit_rate_tolerance = avio_rb32(pb);
427  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
428  codec->rc_eq = av_strdup(rc_eq_buf);
429  codec->rc_max_rate = avio_rb32(pb);
430  codec->rc_min_rate = avio_rb32(pb);
431  codec->rc_buffer_size = avio_rb32(pb);
432  codec->i_quant_factor = av_int2double(avio_rb64(pb));
433  codec->b_quant_factor = av_int2double(avio_rb64(pb));
434  codec->i_quant_offset = av_int2double(avio_rb64(pb));
435  codec->b_quant_offset = av_int2double(avio_rb64(pb));
436  codec->dct_algo = avio_rb32(pb);
437  codec->strict_std_compliance = avio_rb32(pb);
438  codec->max_b_frames = avio_rb32(pb);
439  codec->mpeg_quant = avio_rb32(pb);
440  codec->intra_dc_precision = avio_rb32(pb);
441  codec->me_method = avio_rb32(pb);
442  codec->mb_decision = avio_rb32(pb);
443  codec->nsse_weight = avio_rb32(pb);
444  codec->frame_skip_cmp = avio_rb32(pb);
446  codec->codec_tag = avio_rb32(pb);
447  codec->thread_count = avio_r8(pb);
448  codec->coder_type = avio_rb32(pb);
449  codec->me_cmp = avio_rb32(pb);
450  codec->me_subpel_quality = avio_rb32(pb);
451  codec->me_range = avio_rb32(pb);
452  codec->keyint_min = avio_rb32(pb);
453  codec->scenechange_threshold = avio_rb32(pb);
454  codec->b_frame_strategy = avio_rb32(pb);
455  codec->qcompress = av_int2double(avio_rb64(pb));
456  codec->qblur = av_int2double(avio_rb64(pb));
457  codec->max_qdiff = avio_rb32(pb);
458  codec->refs = avio_rb32(pb);
459  break;
460  case AVMEDIA_TYPE_AUDIO:
461  codec->sample_rate = avio_rb32(pb);
462  codec->channels = avio_rl16(pb);
463  codec->frame_size = avio_rl16(pb);
464  break;
465  default:
466  goto fail;
467  }
468  if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
469  if (ff_alloc_extradata(codec, avio_rb32(pb)))
470  return AVERROR(ENOMEM);
471  avio_read(pb, codec->extradata, codec->extradata_size);
472  }
473  }
474 
475  /* get until end of block reached */
476  while ((avio_tell(pb) % ffm->packet_size) != 0)
477  avio_r8(pb);
478 
479  /* init packet demux */
480  ffm->packet_ptr = ffm->packet;
481  ffm->packet_end = ffm->packet;
482  ffm->frame_offset = 0;
483  ffm->dts = 0;
484  ffm->read_state = READ_HEADER;
485  ffm->first_packet = 1;
486  return 0;
487  fail:
488  ffm_close(s);
489  return -1;
490 }
491 
492 /* return < 0 if eof */
494 {
495  int size;
496  FFMContext *ffm = s->priv_data;
497  int duration, ret;
498 
499  switch(ffm->read_state) {
500  case READ_HEADER:
501  if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
502  return ret;
503 
504  av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
505  avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
506  if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
508  return -1;
509  if (ffm->header[1] & FLAG_DTS)
510  if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
511  return -1;
512  ffm->read_state = READ_DATA;
513  /* fall thru */
514  case READ_DATA:
515  size = AV_RB24(ffm->header + 2);
516  if ((ret = ffm_is_avail_data(s, size)) < 0)
517  return ret;
518 
519  duration = AV_RB24(ffm->header + 5);
520 
521  if (av_new_packet(pkt, size) < 0) {
522  return AVERROR(ENOMEM);
523  }
524  pkt->stream_index = ffm->header[0];
525  if ((unsigned)pkt->stream_index >= s->nb_streams) {
526  av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
527  av_free_packet(pkt);
528  ffm->read_state = READ_HEADER;
529  return -1;
530  }
531  pkt->pos = avio_tell(s->pb);
532  if (ffm->header[1] & FLAG_KEY_FRAME)
533  pkt->flags |= AV_PKT_FLAG_KEY;
534 
535  ffm->read_state = READ_HEADER;
536  if (ffm_read_data(s, pkt->data, size, 0) != size) {
537  /* bad case: desynchronized packet. we cancel all the packet loading */
538  av_free_packet(pkt);
539  return -1;
540  }
541  pkt->pts = AV_RB64(ffm->header+8);
542  if (ffm->header[1] & FLAG_DTS)
543  pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
544  else
545  pkt->dts = pkt->pts;
546  pkt->duration = duration;
547  break;
548  }
549  return 0;
550 }
551 
552 /* seek to a given time in the file. The file read pointer is
553  positioned at or before pts. XXX: the following code is quite
554  approximative */
555 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
556 {
557  FFMContext *ffm = s->priv_data;
558  int64_t pos_min, pos_max, pos;
559  int64_t pts_min, pts_max, pts;
560  double pos1;
561 
562  av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
563  /* find the position using linear interpolation (better than
564  dichotomy in typical cases) */
565  if (ffm->write_index && ffm->write_index < ffm->file_size) {
566  if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
567  pos_min = FFM_PACKET_SIZE;
568  pos_max = ffm->write_index - FFM_PACKET_SIZE;
569  } else {
570  pos_min = ffm->write_index;
571  pos_max = ffm->file_size - FFM_PACKET_SIZE;
572  }
573  } else {
574  pos_min = FFM_PACKET_SIZE;
575  pos_max = ffm->file_size - FFM_PACKET_SIZE;
576  }
577  while (pos_min <= pos_max) {
578  pts_min = get_dts(s, pos_min);
579  pts_max = get_dts(s, pos_max);
580  if (pts_min > wanted_pts || pts_max <= wanted_pts) {
581  pos = pts_min > wanted_pts ? pos_min : pos_max;
582  goto found;
583  }
584  /* linear interpolation */
585  pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
586  (double)(pts_max - pts_min);
587  pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
588  if (pos <= pos_min)
589  pos = pos_min;
590  else if (pos >= pos_max)
591  pos = pos_max;
592  pts = get_dts(s, pos);
593  /* check if we are lucky */
594  if (pts == wanted_pts) {
595  goto found;
596  } else if (pts > wanted_pts) {
597  pos_max = pos - FFM_PACKET_SIZE;
598  } else {
599  pos_min = pos + FFM_PACKET_SIZE;
600  }
601  }
602  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
603 
604  found:
605  if (ffm_seek1(s, pos) < 0)
606  return -1;
607 
608  /* reset read state */
609  ffm->read_state = READ_HEADER;
610  ffm->packet_ptr = ffm->packet;
611  ffm->packet_end = ffm->packet;
612  ffm->first_packet = 1;
613 
614  return 0;
615 }
616 
617 static int ffm_probe(AVProbeData *p)
618 {
619  if (
620  p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
621  (p->buf[3] == '1' || p->buf[3] == '2'))
622  return AVPROBE_SCORE_MAX + 1;
623  return 0;
624 }
625 
627  .name = "ffm",
628  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
629  .priv_data_size = sizeof(FFMContext),
634  .read_seek = ffm_seek,
635 };