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