1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
26
30
32 {
35
40
41 return 0;
42 }
43
46 int is_ref, int in_dpb, int prev)
47 {
48 int refs = 0;
49
50 if (is_ref) {
56 else
58 ++refs;
59 }
60
61 if (in_dpb) {
64 ++refs;
65 }
66
67 if (prev) {
70 ++refs;
71 }
72
75 }
76
78 {
80
82 return;
83
88 }
89
94 }
95
100 }
101
106 }
107
109 }
110
115 int current_depth,
117 {
120
122
123 // If we are at the maximum depth then encode all pictures as
124 // non-referenced B-pictures. Also do this if there is exactly one
125 // picture left, since there will be nothing to reference it.
126 if (current_depth ==
ctx->max_b_depth || start->
next->
next == end) {
127 for (pic = start->
next; pic; pic = pic->
next) {
128 if (pic == end)
129 break;
132
136
139 }
140 *last = prev;
141
142 } else {
143 // Split the current list at the midpoint with a referenced
144 // B-picture, then descend into each side separately.
146 for (pic = start->
next; pic != end; pic = pic->
next)
148 for (pic = start->
next,
i = 1; 2 * i < len; pic = pic->next,
i++);
149
152
154
159
162
165 current_depth + 1, &next);
166 else
167 next = pic;
168
170 current_depth + 1, last);
171 }
172 }
173
176 {
178
179 if (!pic)
180 return;
181
183 for (
i = 0;
i <
ctx->nb_next_prev;
i++) {
184 --
ctx->next_prev[
i]->ref_count[0];
186 }
187 ctx->next_prev[0] = pic;
189 ctx->nb_next_prev = 1;
190
191 return;
192 }
193
194 if (
ctx->nb_next_prev <
ctx->ref_l0) {
195 ctx->next_prev[
ctx->nb_next_prev++] = pic;
197 } else {
198 --
ctx->next_prev[0]->ref_count[0];
199 for (
i = 0;
i <
ctx->ref_l0 - 1;
i++)
200 ctx->next_prev[
i] =
ctx->next_prev[
i + 1];
201 ctx->next_prev[
i] = pic;
203 }
204 }
205
209 {
211 int i, b_counter, closed_gop_end;
212
213 // If there are any B-frames already queued, the next one to encode
214 // is the earliest not-yet-issued frame for which all references are
215 // available.
216 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
218 continue;
220 continue;
223 break;
224 }
226 continue;
227
230 break;
231 }
233 break;
234 }
235
236 if (pic) {
238 "encode next.\n", pic->
b_depth);
239 *pic_out = pic;
240 return 0;
241 }
242
243 // Find the B-per-Pth available picture to become the next picture
244 // on the top layer.
246 b_counter = 0;
247 closed_gop_end =
ctx->closed_gop ||
248 ctx->idr_counter ==
ctx->gop_per_idr;
249 for (pic =
ctx->pic_start; pic; pic = next) {
252 start = pic;
253 continue;
254 }
255 // If the next available picture is force-IDR, encode it to start
256 // a new GOP immediately.
258 break;
259 if (b_counter ==
ctx->b_per_p)
260 break;
261 // If this picture ends a closed GOP or starts a new GOP then it
262 // needs to be in the top layer.
263 if (
ctx->gop_counter + b_counter + closed_gop_end >=
ctx->gop_size)
264 break;
265 // If the picture after this one is force-IDR, we need to encode
266 // this one in the top layer.
267 if (next && next->force_idr)
268 break;
269 ++b_counter;
270 }
271
272 // At the end of the stream the last picture must be in the top layer.
273 if (!pic &&
ctx->end_of_stream) {
274 --b_counter;
280 }
281
282 if (!pic) {
284 "need more input for reference pictures.\n");
286 }
287 if (
ctx->input_order <=
ctx->decode_delay && !
ctx->end_of_stream) {
289 "need more input for timestamps.\n");
291 }
292
295 "encode next.\n");
297 ctx->idr_counter = 1;
298 ctx->gop_counter = 1;
299
300 }
else if (
ctx->gop_counter + b_counter >=
ctx->gop_size) {
301 if (
ctx->idr_counter ==
ctx->gop_per_idr) {
303 "encode next.\n");
305 ctx->idr_counter = 1;
306 } else {
308 "encode next.\n");
311 }
312 ctx->gop_counter = 1;
313
314 } else {
315 if (
ctx->gop_counter + b_counter + closed_gop_end ==
ctx->gop_size) {
317 "encode next.\n");
318 } else {
320 "encode next.\n");
321 }
324 ctx->gop_counter += 1 + b_counter;
325 }
327 *pic_out = pic;
328
331 // TODO: apply both previous and forward multi reference for all vaapi encoders.
332 // And L0/L1 reference frame number can be set dynamically through query
333 // VAConfigAttribEncMaxRefFrames attribute.
335 for (
i = 0;
i <
ctx->nb_next_prev;
i++)
338 b_counter > 0, 0);
339 } else
342 b_counter > 0, 0);
343
345 }
346
347 if (b_counter > 0) {
349 &prev);
350 } else {
351 prev = pic;
352 }
354
355 return 0;
356 }
357
359 {
361
363
364 // Remove direct references once each picture is complete.
365 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
368 }
369
370 // Remove indirect references once a picture has no direct references.
371 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
374 }
375
376 // Clear out all complete pictures with no remaining references.
378 for (pic =
ctx->pic_start; pic; pic = next) {
382 if (prev)
384 else
385 ctx->pic_start = next;
386 ctx->op->free(avctx, pic);
388 } else {
389 prev = pic;
390 }
391 }
392
393 return 0;
394 }
395
398 {
400 frame->crop_left ||
frame->crop_right) && !
ctx->crop_warned) {
402 "frames ignored due to lack of API support.\n");
403 ctx->crop_warned = 1;
404 }
405
406 if (!
ctx->roi_allowed) {
409
410 if (sd && !
ctx->roi_warned) {
412 "frames ignored due to lack of driver support.\n");
414 }
415 }
416
417 return 0;
418 }
419
422 {
424 int err;
425
429
431 if (err < 0)
432 return err;
433
435 if (!pic)
437
442 }
443
444 if (
ctx->recon_frames_ref) {
449 }
450
452 if (err < 0) {
455 }
456 }
457
462 }
463
466
469
472 if (err < 0)
474
476 }
477
479
480 if (
ctx->input_order == 0)
481 ctx->first_pts = pic->
pts;
482 if (
ctx->input_order ==
ctx->decode_delay)
483 ctx->dts_pts_diff = pic->
pts -
ctx->first_pts;
484 if (
ctx->output_delay > 0)
485 ctx->ts_ring[
ctx->input_order %
486 (3 *
ctx->output_delay +
ctx->async_depth)] = pic->
pts;
487
490
491 if (
ctx->pic_start) {
492 ctx->pic_end->next = pic;
494 } else {
495 ctx->pic_start = pic;
497 }
498
499 err =
ctx->op->init(avctx, pic);
500 if (err < 0)
502 } else {
503 ctx->end_of_stream = 1;
504
505 // Fix timestamps if we hit end-of-stream before the initial decode
506 // delay has elapsed.
507 if (
ctx->input_order <=
ctx->decode_delay)
508 ctx->dts_pts_diff =
ctx->pic_end->pts -
ctx->first_pts;
509 }
510
511 return 0;
512
514 ctx->op->free(avctx, pic);
516 return err;
517 }
518
523 {
526
529
530 // for no-delay encoders this is handled in generic codec
536 }
537
538 if (flag_no_delay) {
540 return 0;
541 }
542
543 if (
ctx->output_delay == 0) {
548 else
550 } else {
552 (3 *
ctx->output_delay +
ctx->async_depth)];
553 }
554
555 return 0;
556 }
557
560 {
563 int err;
564
566 ctx->op->output &&
ctx->op->free);
567
568 start:
569 /** if no B frame before repeat P frame, sent repeat P frame out. */
570 if (
ctx->tail_pkt->size) {
573 break;
574 else if (!
tmp->next) {
576 goto end;
577 }
578 }
579 }
580
584 } else if (err < 0)
585 return err;
586
588 if (err < 0)
589 return err;
590
591 if (!
ctx->pic_start) {
592 if (
ctx->end_of_stream)
594 else
596 }
597
598 if (
ctx->async_encode) {
601 if (!err) {
605 err =
ctx->op->issue(avctx, pic);
606 if (err < 0) {
608 return err;
609 }
612 }
613 }
614
616 return err;
617
618 // More frames can be buffered
621
624 } else {
626 if (err < 0)
627 return err;
629
631
632 err =
ctx->op->issue(avctx, pic);
633 if (err < 0) {
635 return err;
636 }
637
639 }
640
641 err =
ctx->op->output(avctx, pic,
pkt);
642 if (err < 0) {
644 return err;
645 }
646
649
650 /** loop to get an available pkt in encoder flushing. */
652 goto start;
653
654 end:
658
659 return 0;
660 }
661
663 uint32_t ref_l0, uint32_t ref_l1,
664 int flags,
int prediction_pre_only)
665 {
668
669 if (avctx->
refs > 0) {
672 }
673
677 } else if (ref_l0 < 1) {
679 "reference frames.\n");
685 "(supported references: %d / %d).\n",
686 ref_l0, ref_l1);
687 else
689 "(supported references: %d / %d).\n", ref_l0, ref_l1);
691 ctx->p_per_i = INT_MAX;
693 } else {
696 "(supported references: %d / %d).\n",
697 ref_l0, ref_l1);
698 else
700 "(supported references: %d / %d).\n", ref_l0, ref_l1);
702 ctx->p_per_i = INT_MAX;
707 } else {
708 ctx->max_b_depth = 1;
709 }
710 }
711
714 ctx->gop_per_idr =
ctx->idr_interval + 1;
715 } else {
717 ctx->gop_per_idr = 1;
718 }
719
720 return 0;
721 }
722
725 {
729
731 hwconfig);
732 if (!constraints) {
735 }
736
737 // Probably we can use the input surface format as the surface format
738 // of the reconstructed frames. If not, we just pick the first (only?)
739 // format in the valid list and hope that it all works.
743 if (
ctx->input_frames->sw_format ==
745 recon_format =
ctx->input_frames->sw_format;
746 break;
747 }
748 }
750 // No match. Just use the first in the supported list and
751 // hope for the best.
753 }
754 } else {
755 // No idea what to use; copy input format.
756 recon_format =
ctx->input_frames->sw_format;
757 }
760
766 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
767 ctx->surface_width,
ctx->surface_height,
772 }
773
774 *fmt = recon_format;
775 err = 0;
778 return err;
779 }
780
782 {
783 ctx->log_ctx = (
void *)avctx;
784
788
791 "required to associate the encoding device.\n");
793 }
794
796 if (!
ctx->input_frames_ref)
798
800
802 if (!
ctx->device_ref)
804
806
810
811 return 0;
812 }
813
815 {
817 next_pic = pic->next;
819 }
820
822
825
829
830 return 0;
831 }