static int dv_write_header(AVFormatContext *s) { DVMuxContext *c = s->priv_data; if (!dv_init_mux(s)) { av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n" "Make sure that you supply exactly two streams:\n" " video: 25fps or 29.97fps, audio: 2ch/48kHz/PCM BigEndian\n" " (50Mbps allows an optional second audio stream)\n"); return -1; } // default to drop frame timecode for 29.97 fps if (c->sys->time_base.den == 30000 && c->sys->time_base.num == 1001) c->timecode_drop_frame = 1; if (c->timecode) { int drop, framenum; framenum = ff_timecode_to_framenum(c->timecode, c->sys->time_base, &drop); if (framenum < 0) { if (framenum == -1) av_log(s, AV_LOG_ERROR, "error parsing timecode, syntax: 00:00:00[;:]00\n"); else if (framenum == -2) av_log(s, AV_LOG_ERROR, "error, unsupported fps for timecode\n"); else if (framenum == -3) av_log(s, AV_LOG_ERROR, "error, drop frame is only allowed with " "30000/1001 or 60000/1001 fps\n"); return -1; } c->timecode_start = framenum; c->timecode_drop_frame = drop; } return 0; }
static av_cold int encode_init(AVCodecContext *avctx) { MpegEncContext *s = avctx->priv_data; if(avctx->profile == FF_PROFILE_UNKNOWN){ if(avctx->level != FF_LEVEL_UNKNOWN){ av_log(avctx, AV_LOG_ERROR, "Set profile and level\n"); return -1; } avctx->profile = avctx->pix_fmt == PIX_FMT_YUV420P ? 4 : 0; /* Main or 4:2:2 */ } if(avctx->level == FF_LEVEL_UNKNOWN){ if(avctx->profile == 0){ /* 4:2:2 */ if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */ else avctx->level = 2; /* High */ }else{ if(avctx->profile != 1 && avctx->pix_fmt != PIX_FMT_YUV420P){ av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n"); return -1; } if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */ else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */ else avctx->level = 4; /* High */ } } if (avctx->rc_max_rate && !avctx->rc_buffer_size) { int max = 0; if (avctx->profile == 0) { max = avctx->level == 5 ? 9437184 : 47185920; } else if (avctx->profile == 4) { switch (avctx->level) { case 8: max = 1835008; break; case 6: max = 7340032; break; case 4: max = 9781248; break; } } else { switch (avctx->level) { case 8: max = 2441216; break; case 6: max = 9781248; break; case 4: max = 12222464; break; } } avctx->rc_buffer_size = FFMIN(max, avctx->rc_max_rate*65535LL/90000); } switch (avctx->color_primaries) { case AVCOL_PRI_BT709: avctx->color_transfer = AVCOL_TRC_BT709; avctx->color_matrix = AVCOL_MTX_BT709; break; case AVCOL_PRI_SMPTE170M: case AVCOL_PRI_BT470BG: avctx->color_transfer = AVCOL_TRC_BT709; avctx->color_matrix = AVCOL_MTX_SMPTE170M; break; } if(MPV_encode_init(avctx) < 0) return -1; if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) { float best_aspect_error = 1E10; float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio); int i; for (i = 1; i < 15; i++) { float error = aspect_ratio; if (s->codec_id == CODEC_ID_MPEG1VIDEO || i <= 1) error -= 1.0/ff_mpeg1_aspect[i]; else error -= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width; error = FFABS(error); // <= so square pixels can match 4:3 or 16:9 if (error <= best_aspect_error) { best_aspect_error = error; s->aspect_ratio_info = i; } } } if (!s->aspect_ratio_info) s->aspect_ratio_info = 1; if(find_frame_rate_index(s) < 0){ if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){ av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num); return -1; }else{ av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num); } } if (s->timecode) { int drop, framenum; AVRational fps = ff_frame_rate_tab[s->frame_rate_index]; framenum = ff_timecode_to_framenum(s->timecode, (AVRational){fps.den, fps.num}, &drop); if (framenum < 0) { if (framenum == -1) av_log(s, AV_LOG_ERROR, "error parsing timecode, syntax: 00:00:00[;:]00\n"); else if (framenum == -2) av_log(s, AV_LOG_ERROR, "error, unsupported fps for timecode\n"); else if (framenum == -3) av_log(s, AV_LOG_ERROR, "error, drop frame is only allowed with " "30000/1001 or 60000/1001 fps\n"); return -1; } s->timecode_start = framenum; s->timecode_drop_frame = drop; } if (s->pulldown) { if (strcmp(s->pulldown, "3:2")) { av_log(s, AV_LOG_ERROR, "error, unknown pulldown value\n"); return -1; } if (s->frame_rate_index != 1) { av_log(s, AV_LOG_ERROR, "error, pulldown only works with 24000/1001 fps\n"); return -1; } s->frame_rate_index = 4; // override frame rate to 30000/1001 } return 0; }
static av_cold int encode_init(AVCodecContext *avctx) { MpegEncContext *s = avctx->priv_data; if(avctx->profile == FF_PROFILE_UNKNOWN){ if(avctx->level != FF_LEVEL_UNKNOWN){ av_log(avctx, AV_LOG_ERROR, "Set profile and level\n"); return -1; } avctx->profile = avctx->pix_fmt == PIX_FMT_YUV420P ? 4 : 0; /* Main or 4:2:2 */ } if(avctx->level == FF_LEVEL_UNKNOWN){ if(avctx->profile == 0){ /* 4:2:2 */ if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */ else avctx->level = 2; /* High */ }else{ if(avctx->profile != 1 && avctx->pix_fmt != PIX_FMT_YUV420P){ av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n"); return -1; } if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */ else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */ else avctx->level = 4; /* High */ } } if (avctx->rc_max_rate && !avctx->rc_buffer_size) { int max = 0; if (avctx->profile == 0) { max = avctx->level == 5 ? 9437184 : 47185920; } else if (avctx->profile == 4) { switch (avctx->level) { case 8: max = 1835008; break; case 6: max = 7340032; break; case 4: max = 9781248; break; } } else { switch (avctx->level) { case 8: max = 2441216; break; case 6: max = 9781248; break; case 4: max = 12222464; break; } } avctx->rc_buffer_size = FFMIN(max, avctx->rc_max_rate*65535LL/90000); avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size*3/4; } switch (avctx->color_primaries) { case AVCOL_PRI_BT709: avctx->color_transfer = AVCOL_TRC_BT709; avctx->color_matrix = AVCOL_MTX_BT709; break; case AVCOL_PRI_SMPTE170M: case AVCOL_PRI_BT470BG: avctx->color_transfer = AVCOL_TRC_BT709; avctx->color_matrix = AVCOL_MTX_SMPTE170M; break; } if(MPV_encode_init(avctx) < 0) return -1; if(find_frame_rate_index(s) < 0){ if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){ av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num); return -1; }else{ av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num); } } if (s->timecode) { int drop, framenum; AVRational fps = ff_frame_rate_tab[s->frame_rate_index]; framenum = ff_timecode_to_framenum(s->timecode, (AVRational){fps.den, fps.num}, &drop); if (framenum < 0) { if (framenum == -1) av_log(s, AV_LOG_ERROR, "error parsing timecode, syntax: 00:00:00[;:]00\n"); else if (framenum == -2) av_log(s, AV_LOG_ERROR, "error, unsupported fps for timecode\n"); else if (framenum == -3) av_log(s, AV_LOG_ERROR, "error, drop frame is only allowed with " "30000/1001 or 60000/1001 fps\n"); return -1; } s->timecode_start = framenum; s->timecode_drop_frame = drop; } return 0; }