00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/intfloat.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "ffm.h"
00027 #include "avio_internal.h"
00028 #if CONFIG_FFSERVER
00029 #include <unistd.h>
00030
00031 int64_t ffm_read_write_index(int fd)
00032 {
00033 uint8_t buf[8];
00034
00035 lseek(fd, 8, SEEK_SET);
00036 if (read(fd, buf, 8) != 8)
00037 return AVERROR(EIO);
00038 return AV_RB64(buf);
00039 }
00040
00041 int ffm_write_write_index(int fd, int64_t pos)
00042 {
00043 uint8_t buf[8];
00044 int i;
00045
00046 for(i=0;i<8;i++)
00047 buf[i] = (pos >> (56 - i * 8)) & 0xff;
00048 lseek(fd, 8, SEEK_SET);
00049 if (write(fd, buf, 8) != 8)
00050 return AVERROR(EIO);
00051 return 8;
00052 }
00053
00054 void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
00055 {
00056 FFMContext *ffm = s->priv_data;
00057 ffm->write_index = pos;
00058 ffm->file_size = file_size;
00059 }
00060 #endif // CONFIG_FFSERVER
00061
00062 static int ffm_is_avail_data(AVFormatContext *s, int size)
00063 {
00064 FFMContext *ffm = s->priv_data;
00065 int64_t pos, avail_size;
00066 int len;
00067
00068 len = ffm->packet_end - ffm->packet_ptr;
00069 if (size <= len)
00070 return 1;
00071 pos = avio_tell(s->pb);
00072 if (!ffm->write_index) {
00073 if (pos == ffm->file_size)
00074 return AVERROR_EOF;
00075 avail_size = ffm->file_size - pos;
00076 } else {
00077 if (pos == ffm->write_index) {
00078
00079 return AVERROR(EAGAIN);
00080 } else if (pos < ffm->write_index) {
00081 avail_size = ffm->write_index - pos;
00082 } else {
00083 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
00084 }
00085 }
00086 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
00087 if (size <= avail_size)
00088 return 1;
00089 else
00090 return AVERROR(EAGAIN);
00091 }
00092
00093 static int ffm_resync(AVFormatContext *s, int state)
00094 {
00095 av_log(s, AV_LOG_ERROR, "resyncing\n");
00096 while (state != PACKET_ID) {
00097 if (url_feof(s->pb)) {
00098 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
00099 return -1;
00100 }
00101 state = (state << 8) | avio_r8(s->pb);
00102 }
00103 return 0;
00104 }
00105
00106
00107 static int ffm_read_data(AVFormatContext *s,
00108 uint8_t *buf, int size, int header)
00109 {
00110 FFMContext *ffm = s->priv_data;
00111 AVIOContext *pb = s->pb;
00112 int len, fill_size, size1, frame_offset, id;
00113
00114 size1 = size;
00115 while (size > 0) {
00116 redo:
00117 len = ffm->packet_end - ffm->packet_ptr;
00118 if (len < 0)
00119 return -1;
00120 if (len > size)
00121 len = size;
00122 if (len == 0) {
00123 if (avio_tell(pb) == ffm->file_size)
00124 avio_seek(pb, ffm->packet_size, SEEK_SET);
00125 retry_read:
00126 if (pb->buffer_size != ffm->packet_size) {
00127 int64_t tell = avio_tell(pb);
00128 ffio_set_buf_size(pb, ffm->packet_size);
00129 avio_seek(pb, tell, SEEK_SET);
00130 }
00131 id = avio_rb16(pb);
00132 if (id != PACKET_ID)
00133 if (ffm_resync(s, id) < 0)
00134 return -1;
00135 fill_size = avio_rb16(pb);
00136 ffm->dts = avio_rb64(pb);
00137 frame_offset = avio_rb16(pb);
00138 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
00139 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
00140 if (ffm->packet_end < ffm->packet || frame_offset < 0)
00141 return -1;
00142
00143
00144 if (ffm->first_packet || (frame_offset & 0x8000)) {
00145 if (!frame_offset) {
00146
00147 if (avio_tell(pb) >= ffm->packet_size * 3) {
00148 avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
00149 goto retry_read;
00150 }
00151
00152 return 0;
00153 }
00154 ffm->first_packet = 0;
00155 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
00156 return -1;
00157 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
00158 if (!header)
00159 break;
00160 } else {
00161 ffm->packet_ptr = ffm->packet;
00162 }
00163 goto redo;
00164 }
00165 memcpy(buf, ffm->packet_ptr, len);
00166 buf += len;
00167 ffm->packet_ptr += len;
00168 size -= len;
00169 header = 0;
00170 }
00171 return size1 - size;
00172 }
00173
00174
00175
00176 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
00177 {
00178 FFMContext *ffm = s->priv_data;
00179 AVIOContext *pb = s->pb;
00180 int64_t pos;
00181
00182 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
00183 pos = FFMAX(pos, FFM_PACKET_SIZE);
00184 av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
00185 return avio_seek(pb, pos, SEEK_SET);
00186 }
00187
00188 static int64_t get_dts(AVFormatContext *s, int64_t pos)
00189 {
00190 AVIOContext *pb = s->pb;
00191 int64_t dts;
00192
00193 ffm_seek1(s, pos);
00194 avio_skip(pb, 4);
00195 dts = avio_rb64(pb);
00196 av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
00197 return dts;
00198 }
00199
00200 static void adjust_write_index(AVFormatContext *s)
00201 {
00202 FFMContext *ffm = s->priv_data;
00203 AVIOContext *pb = s->pb;
00204 int64_t pts;
00205
00206 int64_t pos_min, pos_max;
00207 int64_t pts_start;
00208 int64_t ptr = avio_tell(pb);
00209
00210
00211 pos_min = 0;
00212 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
00213
00214 pts_start = get_dts(s, pos_min);
00215
00216 pts = get_dts(s, pos_max);
00217
00218 if (pts - 100000 > pts_start)
00219 goto end;
00220
00221 ffm->write_index = FFM_PACKET_SIZE;
00222
00223 pts_start = get_dts(s, pos_min);
00224
00225 pts = get_dts(s, pos_max);
00226
00227 if (pts - 100000 <= pts_start) {
00228 while (1) {
00229 int64_t newpos;
00230 int64_t newpts;
00231
00232 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
00233
00234 if (newpos == pos_min)
00235 break;
00236
00237 newpts = get_dts(s, newpos);
00238
00239 if (newpts - 100000 <= pts) {
00240 pos_max = newpos;
00241 pts = newpts;
00242 } else {
00243 pos_min = newpos;
00244 }
00245 }
00246 ffm->write_index += pos_max;
00247 }
00248
00249
00250
00251
00252 end:
00253 avio_seek(pb, ptr, SEEK_SET);
00254 }
00255
00256
00257 static int ffm_close(AVFormatContext *s)
00258 {
00259 int i;
00260
00261 for (i = 0; i < s->nb_streams; i++)
00262 av_freep(&s->streams[i]->codec->rc_eq);
00263
00264 return 0;
00265 }
00266
00267
00268 static int ffm_read_header(AVFormatContext *s)
00269 {
00270 FFMContext *ffm = s->priv_data;
00271 AVStream *st;
00272 AVIOContext *pb = s->pb;
00273 AVCodecContext *codec;
00274 int i, nb_streams;
00275 uint32_t tag;
00276
00277
00278 tag = avio_rl32(pb);
00279 if (tag != MKTAG('F', 'F', 'M', '1'))
00280 goto fail;
00281 ffm->packet_size = avio_rb32(pb);
00282 if (ffm->packet_size != FFM_PACKET_SIZE)
00283 goto fail;
00284 ffm->write_index = avio_rb64(pb);
00285
00286 if (pb->seekable) {
00287 ffm->file_size = avio_size(pb);
00288 if (ffm->write_index && 0)
00289 adjust_write_index(s);
00290 } else {
00291 ffm->file_size = (UINT64_C(1) << 63) - 1;
00292 }
00293
00294 nb_streams = avio_rb32(pb);
00295 avio_rb32(pb);
00296
00297 for(i=0;i<nb_streams;i++) {
00298 char rc_eq_buf[128];
00299
00300 st = avformat_new_stream(s, NULL);
00301 if (!st)
00302 goto fail;
00303
00304 avpriv_set_pts_info(st, 64, 1, 1000000);
00305
00306 codec = st->codec;
00307
00308 codec->codec_id = avio_rb32(pb);
00309 codec->codec_type = avio_r8(pb);
00310 codec->bit_rate = avio_rb32(pb);
00311 codec->flags = avio_rb32(pb);
00312 codec->flags2 = avio_rb32(pb);
00313 codec->debug = avio_rb32(pb);
00314
00315 switch(codec->codec_type) {
00316 case AVMEDIA_TYPE_VIDEO:
00317 codec->time_base.num = avio_rb32(pb);
00318 codec->time_base.den = avio_rb32(pb);
00319 codec->width = avio_rb16(pb);
00320 codec->height = avio_rb16(pb);
00321 codec->gop_size = avio_rb16(pb);
00322 codec->pix_fmt = avio_rb32(pb);
00323 codec->qmin = avio_r8(pb);
00324 codec->qmax = avio_r8(pb);
00325 codec->max_qdiff = avio_r8(pb);
00326 codec->qcompress = avio_rb16(pb) / 10000.0;
00327 codec->qblur = avio_rb16(pb) / 10000.0;
00328 codec->bit_rate_tolerance = avio_rb32(pb);
00329 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
00330 codec->rc_eq = av_strdup(rc_eq_buf);
00331 codec->rc_max_rate = avio_rb32(pb);
00332 codec->rc_min_rate = avio_rb32(pb);
00333 codec->rc_buffer_size = avio_rb32(pb);
00334 codec->i_quant_factor = av_int2double(avio_rb64(pb));
00335 codec->b_quant_factor = av_int2double(avio_rb64(pb));
00336 codec->i_quant_offset = av_int2double(avio_rb64(pb));
00337 codec->b_quant_offset = av_int2double(avio_rb64(pb));
00338 codec->dct_algo = avio_rb32(pb);
00339 codec->strict_std_compliance = avio_rb32(pb);
00340 codec->max_b_frames = avio_rb32(pb);
00341 codec->mpeg_quant = avio_rb32(pb);
00342 codec->intra_dc_precision = avio_rb32(pb);
00343 codec->me_method = avio_rb32(pb);
00344 codec->mb_decision = avio_rb32(pb);
00345 codec->nsse_weight = avio_rb32(pb);
00346 codec->frame_skip_cmp = avio_rb32(pb);
00347 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
00348 codec->codec_tag = avio_rb32(pb);
00349 codec->thread_count = avio_r8(pb);
00350 codec->coder_type = avio_rb32(pb);
00351 codec->me_cmp = avio_rb32(pb);
00352 codec->me_subpel_quality = avio_rb32(pb);
00353 codec->me_range = avio_rb32(pb);
00354 codec->keyint_min = avio_rb32(pb);
00355 codec->scenechange_threshold = avio_rb32(pb);
00356 codec->b_frame_strategy = avio_rb32(pb);
00357 codec->qcompress = av_int2double(avio_rb64(pb));
00358 codec->qblur = av_int2double(avio_rb64(pb));
00359 codec->max_qdiff = avio_rb32(pb);
00360 codec->refs = avio_rb32(pb);
00361 break;
00362 case AVMEDIA_TYPE_AUDIO:
00363 codec->sample_rate = avio_rb32(pb);
00364 codec->channels = avio_rl16(pb);
00365 codec->frame_size = avio_rl16(pb);
00366 codec->sample_fmt = (int16_t) avio_rl16(pb);
00367 break;
00368 default:
00369 goto fail;
00370 }
00371 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00372 codec->extradata_size = avio_rb32(pb);
00373 codec->extradata = av_malloc(codec->extradata_size);
00374 if (!codec->extradata)
00375 return AVERROR(ENOMEM);
00376 avio_read(pb, codec->extradata, codec->extradata_size);
00377 }
00378 }
00379
00380
00381 while ((avio_tell(pb) % ffm->packet_size) != 0)
00382 avio_r8(pb);
00383
00384
00385 ffm->packet_ptr = ffm->packet;
00386 ffm->packet_end = ffm->packet;
00387 ffm->frame_offset = 0;
00388 ffm->dts = 0;
00389 ffm->read_state = READ_HEADER;
00390 ffm->first_packet = 1;
00391 return 0;
00392 fail:
00393 ffm_close(s);
00394 return -1;
00395 }
00396
00397
00398 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
00399 {
00400 int size;
00401 FFMContext *ffm = s->priv_data;
00402 int duration, ret;
00403
00404 switch(ffm->read_state) {
00405 case READ_HEADER:
00406 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
00407 return ret;
00408
00409 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
00410 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
00411 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
00412 FRAME_HEADER_SIZE)
00413 return -1;
00414 if (ffm->header[1] & FLAG_DTS)
00415 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
00416 return -1;
00417 ffm->read_state = READ_DATA;
00418
00419 case READ_DATA:
00420 size = AV_RB24(ffm->header + 2);
00421 if ((ret = ffm_is_avail_data(s, size)) < 0)
00422 return ret;
00423
00424 duration = AV_RB24(ffm->header + 5);
00425
00426 av_new_packet(pkt, size);
00427 pkt->stream_index = ffm->header[0];
00428 if ((unsigned)pkt->stream_index >= s->nb_streams) {
00429 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
00430 av_free_packet(pkt);
00431 ffm->read_state = READ_HEADER;
00432 return -1;
00433 }
00434 pkt->pos = avio_tell(s->pb);
00435 if (ffm->header[1] & FLAG_KEY_FRAME)
00436 pkt->flags |= AV_PKT_FLAG_KEY;
00437
00438 ffm->read_state = READ_HEADER;
00439 if (ffm_read_data(s, pkt->data, size, 0) != size) {
00440
00441 av_free_packet(pkt);
00442 return -1;
00443 }
00444 pkt->pts = AV_RB64(ffm->header+8);
00445 if (ffm->header[1] & FLAG_DTS)
00446 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
00447 else
00448 pkt->dts = pkt->pts;
00449 pkt->duration = duration;
00450 break;
00451 }
00452 return 0;
00453 }
00454
00455
00456
00457
00458 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
00459 {
00460 FFMContext *ffm = s->priv_data;
00461 int64_t pos_min, pos_max, pos;
00462 int64_t pts_min, pts_max, pts;
00463 double pos1;
00464
00465 av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
00466
00467
00468 if (ffm->write_index && ffm->write_index < ffm->file_size) {
00469 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
00470 pos_min = FFM_PACKET_SIZE;
00471 pos_max = ffm->write_index - FFM_PACKET_SIZE;
00472 } else {
00473 pos_min = ffm->write_index;
00474 pos_max = ffm->file_size - FFM_PACKET_SIZE;
00475 }
00476 } else {
00477 pos_min = FFM_PACKET_SIZE;
00478 pos_max = ffm->file_size - FFM_PACKET_SIZE;
00479 }
00480 while (pos_min <= pos_max) {
00481 pts_min = get_dts(s, pos_min);
00482 pts_max = get_dts(s, pos_max);
00483 if (pts_min > wanted_pts || pts_max < wanted_pts) {
00484 pos = pts_min > wanted_pts ? pos_min : pos_max;
00485 goto found;
00486 }
00487
00488 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
00489 (double)(pts_max - pts_min);
00490 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
00491 if (pos <= pos_min)
00492 pos = pos_min;
00493 else if (pos >= pos_max)
00494 pos = pos_max;
00495 pts = get_dts(s, pos);
00496
00497 if (pts == wanted_pts) {
00498 goto found;
00499 } else if (pts > wanted_pts) {
00500 pos_max = pos - FFM_PACKET_SIZE;
00501 } else {
00502 pos_min = pos + FFM_PACKET_SIZE;
00503 }
00504 }
00505 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
00506
00507 found:
00508 if (ffm_seek1(s, pos) < 0)
00509 return -1;
00510
00511
00512 ffm->read_state = READ_HEADER;
00513 ffm->packet_ptr = ffm->packet;
00514 ffm->packet_end = ffm->packet;
00515 ffm->first_packet = 1;
00516
00517 return 0;
00518 }
00519
00520 static int ffm_probe(AVProbeData *p)
00521 {
00522 if (
00523 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
00524 p->buf[3] == '1')
00525 return AVPROBE_SCORE_MAX + 1;
00526 return 0;
00527 }
00528
00529 AVInputFormat ff_ffm_demuxer = {
00530 .name = "ffm",
00531 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
00532 .priv_data_size = sizeof(FFMContext),
00533 .read_probe = ffm_probe,
00534 .read_header = ffm_read_header,
00535 .read_packet = ffm_read_packet,
00536 .read_close = ffm_close,
00537 .read_seek = ffm_seek,
00538 };