1 /*
2 * HEVC video decoder
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
26
30
32 {
33 /* frame->frame can be NULL if context init failed */
35 return;
36
37 frame->
flags &= ~flags;
40
43
48
50
53 }
54 }
55
57 {
63 }
64
66 {
67 int i;
72 }
73
75 {
76 int i;
79 }
80
82 {
83 int i, j, ret;
87 continue;
88
91 if (ret < 0)
93
97
102
110
113
122 }
123 }
124
129 }
132 }
133
135 {
137 int i;
138
139 /* check that this POC doesn't already exist */
142
146 poc);
148 }
149 }
150
152 if (!ref)
154
157
160 else
162
166
167 return 0;
168 }
169
171 {
172 do {
173 int nb_output = 0;
174 int min_poc = INT_MAX;
175 int i, min_idx, ret;
176
183 }
184 }
185 }
186
191 nb_output++;
192 if (frame->
poc < min_poc || nb_output == 1) {
193 min_poc = frame->
poc;
194 min_idx = i;
195 }
196 }
197 }
198
199 /* wait for more frames before output */
202 return 0;
203
204 if (nb_output) {
209 int pixel_shift = !!(desc->
comp[0].
depth > 8);
210
214 else
216 if (ret < 0)
217 return ret;
218
219 for (i = 0; i < 3; i++) {
225 }
227 "Output frame with POC %d.\n", frame->
poc);
228 return 1;
229 }
230
233 else
234 break;
235 } while (1);
236
237 return 0;
238 }
239
241 {
242 int dpb = 0;
243 int min_poc = INT_MAX;
244 int i;
245
248 if ((frame->
flags) &&
251 dpb++;
252 }
253 }
254
258 if ((frame->
flags) &&
262 min_poc = frame->
poc;
263 }
264 }
265 }
266
271 frame->
poc <= min_poc) {
273 }
274 }
275
276 dpb--;
277 }
278 }
279
281 {
285 int i;
286
289
290 for (i = ctb_addr_ts; i < ctb_count; i++)
292
294
295 return 0;
296 }
297
299 {
301
304 int i, j, ret;
305
307 if (ret < 0)
308 return ret;
309
314 }
315
316 for (list_idx = 0; list_idx < nb_list; list_idx++) {
319
320 /* The order of the elements is
321 * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
322 * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
326
327 /* concatenate the candidate lists for the current frame */
336 }
337 }
338 }
339
340 /* reorder the references if necessary */
342 for (i = 0; i < sh->
nb_refs[list_idx]; i++) {
344
348 }
349
351 rpl->
ref[i] = rpl_tmp.
ref[idx];
354 }
355 } else {
356 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
358 }
359
363 }
364
365 return 0;
366 }
367
369 {
370 int i;
372
376 if ((ref->
poc & LtMask) == poc)
377 return ref;
378 }
379 }
380
384 if (ref->
poc == poc || (ref->
poc & LtMask) == poc)
385 return ref;
386 }
387 }
388
391 "Could not find ref with POC %d\n", poc);
393 }
394
396 {
399 }
400
402 {
404 int i, x, y;
405
407 if (!frame)
409
412 for (i = 0; frame->
frame->
buf[i]; i++)
415 } else {
421 }
422 }
423 }
424
428
431
433 }
434
435 /* add a reference with the given poc to the list and mark it as used in DPB */
437 int poc, int ref_flag)
438 {
440
443
444 if (!ref) {
446 if (!ref)
448 }
449
453
455 return 0;
456 }
457
459 {
463 int i, ret = 0;
464
465 if (!short_rps) {
467 return 0;
468 }
469
470 /* clear the reference flags on all frames except the current one */
473
475 continue;
476
478 }
479
481 rps[i].nb_refs = 0;
482
483 /* add the short refs */
486 int list;
487
488 if (!short_rps->
used[i])
490 else if (i < short_rps->num_negative_pics)
492 else
494
496 if (ret < 0)
498 }
499
500 /* add the long refs */
501 for (i = 0; i < long_rps->
nb_refs; i++) {
502 int poc = long_rps->
poc[i];
504
506 if (ret < 0)
508 }
509
511 /* release any frames that are now unused */
514
515 return ret;
516 }
517
519 {
521 int prev_poc_lsb = s->
pocTid0 % max_poc_lsb;
522 int prev_poc_msb = s->
pocTid0 - prev_poc_lsb;
523 int poc_msb;
524
525 if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
526 poc_msb = prev_poc_msb + max_poc_lsb;
527 else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
528 poc_msb = prev_poc_msb - max_poc_lsb;
529 else
530 poc_msb = prev_poc_msb;
531
532 // For BLA picture types, POCmsb is set to 0.
536 poc_msb = 0;
537
538 return poc_msb + poc_lsb;
539 }
540
542 {
543 int ret = 0;
544 int i;
547
548 if (rps) {
550 ret += !!rps->
used[i];
552 ret += !!rps->
used[i];
553 }
554
555 if (long_rps) {
556 for (i = 0; i < long_rps->
nb_refs; i++)
557 ret += !!long_rps->
used[i];
558 }
559 return ret;
560 }
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
static void flush(AVCodecContext *avctx)
int max_dec_pic_buffering
int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
void * hwaccel_picture_private
void ff_hevc_flush_dpb(HEVCContext *s)
Drop all frames currently in DPB.
int ff_hevc_frame_rps(HEVCContext *s)
Construct the reference picture sets for the current frame.
static HEVCFrame * find_ref_idx(HEVCContext *s, int poc)
struct HEVCFrame * ref[MAX_REFS]
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
uint16_t seq_decode
Sequence counters for decoded and output frames, so that old frames are output first after a POC rese...
static HEVCFrame * alloc_frame(HEVCContext *s)
AVBufferPool * rpl_tab_pool
candidate references for the current frame
struct AVHWAccel * hwaccel
Hardware accelerator in use.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
unsigned int num_negative_pics
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Multithreading support functions.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int ff_hevc_slice_rpl(HEVCContext *s)
Construct the reference picture list(s) for the current slice.
int interlaced_frame
The content of the picture is interlaced.
#define HEVC_FRAME_FLAG_LONG_REF
int slice_idx
number of the slice being currently decoded
static void mark_ref(HEVCFrame *frame, int flag)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
unsigned int log2_max_poc_lsb
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
AVBufferRef * rpl_tab_buf
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
unsigned int log2_ctb_size
simple assert() macros that are a bit more flexible than ISO C assert().
uint16_t sequence
A sequence counter, so that old frames are output first after a POC reset.
int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
Compute POC of the current frame and return it.
AVBufferRef * tab_mvf_buf
int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
Find next frame in output order and put a reference to it in frame.
#define HEVC_FRAME_FLAG_SHORT_REF
#define FF_THREAD_FRAME
Decode more than one frame at once.
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
struct HEVCFrame * collocated_ref
static int add_candidate_ref(HEVCContext *s, RefPicList *list, int poc, int ref_flag)
#define FF_ARRAY_ELEMS(a)
static int init_slice_rpl(HEVCContext *s)
static HEVCFrame * generate_missing_ref(HEVCContext *s, int poc)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
#define HEVC_FRAME_FLAG_OUTPUT
void ff_hevc_clear_refs(HEVCContext *s)
Mark all frames in DPB as unused for reference.
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
AVBufferRef * hwaccel_priv_buf
uint8_t * data
The data buffer.
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
void ff_hevc_bump_frame(HEVCContext *s)
enum NALUnitType nal_unit_type
struct HEVCSPS::@52 temporal_layer[MAX_SUB_LAYERS]
uint8_t flags
A combination of HEVC_FRAME_FLAG_*.
int size
Size of data in bytes.
RefPicList * ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *ref, int x0, int y0)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
AVBufferPool * tab_mvf_pool
int top_field_first
If the content is interlaced, is top field displayed first.
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
int ff_hevc_frame_nb_refs(HEVCContext *s)
Get the number of candidate references for the current frame.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
int depth
Number of bits in the component.
#define HEVC_FRAME_FLAG_BUMPING
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.