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