This will allow to reuse it to allocate dummy frames for the second field (which can be a P-field even if the first field was an intra field).
Unified · Split
| @@ -281,14 +281,21 @@ fail: · 280 unmodified lines | ||
| 281 | 281 | return ret; |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | −static int av_cold alloc_dummy_frame(MpegEncContext *s, Picture **picp) | |
| 284 | +static int av_cold alloc_dummy_frame(MpegEncContext *s, Picture **picp, Picture *wpic) | |
| 285 | 285 | { |
| 286 | 286 | Picture *pic; |
| 287 | − int ret = alloc_picture(s, picp, 1); | |
| 287 | + int ret = alloc_picture(s, &pic, 1); | |
| 288 | 288 | if (ret < 0) |
| 289 | 289 | return ret; |
| 290 | 290 | |
| 291 | − pic = *picp; | |
| 291 | + ff_mpeg_unref_picture(wpic); | |
| 292 | + ret = ff_mpeg_ref_picture(wpic, pic); | |
| 293 | + if (ret < 0) { | |
| 294 | + ff_mpeg_unref_picture(pic); | |
| 295 | + return ret; | |
| 296 | + } | |
| 297 | + | |
| 298 | + *picp = pic; | |
| 292 | 299 | |
| 293 | 300 | ff_thread_report_progress(&pic->tf, INT_MAX, 0); |
| 294 | 301 | ff_thread_report_progress(&pic->tf, INT_MAX, 1); |
| @@ -314,6 +321,45 @@ static void color_frame(AVFrame *frame, int luma) · 19 unmodified lines | ||
| 314 | 321 | } |
| 315 | 322 | } |
| 316 | 323 | |
| 324 | +int ff_mpv_alloc_dummy_frames(MpegEncContext *s) | |
| 325 | +{ | |
| 326 | + AVCodecContext *avctx = s->avctx; | |
| 327 | + int ret; | |
| 328 | + | |
| 329 | + if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) && | |
| 330 | + (s->pict_type != AV_PICTURE_TYPE_I)) { | |
| 331 | + if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0]) | |
| 332 | + av_log(avctx, AV_LOG_DEBUG, | |
| 333 | + "allocating dummy last picture for B frame\n"); | |
| 334 | + else if (s->codec_id != AV_CODEC_ID_H261 /* H.261 has no keyframes */ && | |
| 335 | + (s->picture_structure == PICT_FRAME || s->first_field)) | |
| 336 | + av_log(avctx, AV_LOG_ERROR, | |
| 337 | + "warning: first frame is no keyframe\n"); | |
| 338 | + | |
| 339 | + /* Allocate a dummy frame */ | |
| 340 | + ret = alloc_dummy_frame(s, &s->last_picture_ptr, &s->last_picture); | |
| 341 | + if (ret < 0) | |
| 342 | + return ret; | |
| 343 | + | |
| 344 | + if (!avctx->hwaccel) { | |
| 345 | + int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80; | |
| 346 | + color_frame(s->last_picture_ptr->f, luma_val); | |
| 347 | + } | |
| 348 | + } | |
| 349 | + if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) && | |
| 350 | + s->pict_type == AV_PICTURE_TYPE_B) { | |
| 351 | + /* Allocate a dummy frame */ | |
| 352 | + ret = alloc_dummy_frame(s, &s->next_picture_ptr, &s->next_picture); | |
| 353 | + if (ret < 0) | |
| 354 | + return ret; | |
| 355 | + } | |
| 356 | + | |
| 357 | + av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && | |
| 358 | + s->last_picture_ptr->f->buf[0])); | |
| 359 | + | |
| 360 | + return 0; | |
| 361 | +} | |
| 362 | + | |
| 317 | 363 | /** |
| 318 | 364 | * generic function called after decoding |
| 319 | 365 | * the header and before a frame is decoded. |
| @@ -382,34 +428,6 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) · 62 unmodified lines | ||
| 382 | 428 | s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL, |
| 383 | 429 | s->pict_type, s->droppable); |
| 384 | 430 | |
| 385 | − if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) && | |
| 386 | − (s->pict_type != AV_PICTURE_TYPE_I)) { | |
| 387 | − if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0]) | |
| 388 | − av_log(avctx, AV_LOG_DEBUG, | |
| 389 | − "allocating dummy last picture for B frame\n"); | |
| 390 | − else if (s->codec_id != AV_CODEC_ID_H261) | |
| 391 | − av_log(avctx, AV_LOG_ERROR, | |
| 392 | − "warning: first frame is no keyframe\n"); | |
| 393 | − | |
| 394 | − /* Allocate a dummy frame */ | |
| 395 | − ret = alloc_dummy_frame(s, &s->last_picture_ptr); | |
| 396 | − if (ret < 0) | |
| 397 | − return ret; | |
| 398 | − | |
| 399 | − if (!avctx->hwaccel) { | |
| 400 | − int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80; | |
| 401 | − color_frame(s->last_picture_ptr->f, luma_val); | |
| 402 | − } | |
| 403 | − | |
| 404 | − } | |
| 405 | − if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) && | |
| 406 | − s->pict_type == AV_PICTURE_TYPE_B) { | |
| 407 | − /* Allocate a dummy frame */ | |
| 408 | − ret = alloc_dummy_frame(s, &s->next_picture_ptr); | |
| 409 | − if (ret < 0) | |
| 410 | − return ret; | |
| 411 | − } | |
| 412 | − | |
| 413 | 431 | if (s->last_picture_ptr) { |
| 414 | 432 | if (s->last_picture_ptr->f->buf[0] && |
| 415 | 433 | (ret = ff_mpeg_ref_picture(&s->last_picture, |
| @@ -423,8 +441,9 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) · 7 unmodified lines | ||
| 423 | 441 | return ret; |
| 424 | 442 | } |
| 425 | 443 | |
| 426 | − av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && | |
| 427 | − s->last_picture_ptr->f->buf[0])); | |
| 444 | + ret = ff_mpv_alloc_dummy_frames(s); | |
| 445 | + if (ret < 0) | |
| 446 | + return ret; | |
| 428 | 447 | |
| 429 | 448 | /* set dequantizer, we can't do it during init as |
| 430 | 449 | * it might change for MPEG-4 and we can't do it in the header |
| @@ -50,6 +50,10 @@ void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx); · 49 unmodified lines | ||
| 50 | 50 | int ff_mpv_common_frame_size_change(MpegEncContext *s); |
| 51 | 51 | |
| 52 | 52 | int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx); |
| 53 | +/** | |
| 54 | + * Ensure that the dummy frames are allocated according to pict_type if necessary. | |
| 55 | + */ | |
| 56 | +int ff_mpv_alloc_dummy_frames(MpegEncContext *s); | |
| 53 | 57 | void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64]); |
| 54 | 58 | void ff_mpv_report_decode_progress(MpegEncContext *s); |
| 55 | 59 | void ff_mpv_frame_end(MpegEncContext *s); |