FFmpeg
h264_picture.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG-4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "internal.h"
31 #include "cabac.h"
32 #include "cabac_functions.h"
33 #include "error_resilience.h"
34 #include "avcodec.h"
35 #include "h264dec.h"
36 #include "h264data.h"
37 #include "h264chroma.h"
38 #include "h264_mvpred.h"
39 #include "mathops.h"
40 #include "mpegutils.h"
41 #include "rectangle.h"
42 #include "thread.h"
43 
45 {
46  int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
47  int i;
48 
49  if (!pic->f || !pic->f->buf[0])
50  return;
51 
54 
57  for (i = 0; i < 2; i++) {
60  }
61 
62  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
63 }
64 
66 {
67  int ret, i;
68 
69  av_assert0(!dst->f->buf[0]);
70  av_assert0(src->f->buf[0]);
71  av_assert0(src->tf.f == src->f);
72 
73  dst->tf.f = dst->f;
74  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
75  if (ret < 0)
76  goto fail;
77 
80  if (!dst->qscale_table_buf || !dst->mb_type_buf) {
81  ret = AVERROR(ENOMEM);
82  goto fail;
83  }
84  dst->qscale_table = src->qscale_table;
85  dst->mb_type = src->mb_type;
86 
87  for (i = 0; i < 2; i++) {
89  dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
90  if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) {
91  ret = AVERROR(ENOMEM);
92  goto fail;
93  }
94  dst->motion_val[i] = src->motion_val[i];
95  dst->ref_index[i] = src->ref_index[i];
96  }
97 
98  if (src->hwaccel_picture_private) {
100  if (!dst->hwaccel_priv_buf) {
101  ret = AVERROR(ENOMEM);
102  goto fail;
103  }
105  }
106 
107  for (i = 0; i < 2; i++)
108  dst->field_poc[i] = src->field_poc[i];
109 
110  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
111  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
112 
113  dst->poc = src->poc;
114  dst->frame_num = src->frame_num;
115  dst->mmco_reset = src->mmco_reset;
116  dst->long_ref = src->long_ref;
117  dst->mbaff = src->mbaff;
118  dst->field_picture = src->field_picture;
119  dst->reference = src->reference;
120  dst->recovered = src->recovered;
121  dst->invalid_gap = src->invalid_gap;
123 
124  return 0;
125 fail:
126  ff_h264_unref_picture(h, dst);
127  return ret;
128 }
129 
131 {
132 #if CONFIG_ERROR_RESILIENCE
133  int i;
134 
135  memset(dst, 0, sizeof(*dst));
136 
137  if (!src)
138  return;
139 
140  dst->f = src->f;
141  dst->tf = &src->tf;
142 
143  for (i = 0; i < 2; i++) {
144  dst->motion_val[i] = src->motion_val[i];
145  dst->ref_index[i] = src->ref_index[i];
146  }
147 
148  dst->mb_type = src->mb_type;
149  dst->field_picture = src->field_picture;
150 #endif /* CONFIG_ERROR_RESILIENCE */
151 }
152 
154 {
155  AVCodecContext *const avctx = h->avctx;
156  int err = 0;
157  h->mb_y = 0;
158 
159  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
160  if (!h->droppable) {
162  h->poc.prev_poc_msb = h->poc.poc_msb;
163  h->poc.prev_poc_lsb = h->poc.poc_lsb;
164  }
167  }
168 
169  if (avctx->hwaccel) {
170  err = avctx->hwaccel->end_frame(avctx);
171  if (err < 0)
172  av_log(avctx, AV_LOG_ERROR,
173  "hardware accelerator failed to decode picture\n");
174  }
175 
176  if (!in_setup && !h->droppable)
179  emms_c();
180 
181  h->current_slice = 0;
182 
183  return err;
184 }
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:154
int sei_recovery_frame_cnt
Definition: h264dec.h:163
H264POCContext poc
Definition: h264dec.h:460
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:126
misc image utilities
AVFrame * f
Definition: thread.h:35
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:486
int mb_y
Definition: h264dec.h:434
AVBufferRef * mb_type_buf
Definition: h264dec.h:138
int16_t(*[2] motion_val)[2]
Definition: h264dec.h:136
uint32_t * mb_type
H264Context.
Definition: h264dec.h:337
AVFrame * f
Definition: h264dec.h:129
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2785
int picture_structure
Definition: h264dec.h:407
#define tf
Definition: regdef.h:73
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:130
int field_picture
whether or not picture was encoded in separate fields
Definition: h264dec.h:158
int poc
frame POC
Definition: h264dec.h:148
int frame_num_offset
for POC type 2
Definition: h264_parse.h:51
Multithreading support functions.
int invalid_gap
Definition: h264dec.h:162
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1827
AVBufferRef * qscale_table_buf
Definition: h264dec.h:132
int recovered
picture at IDR or recovery point + recovery count
Definition: h264dec.h:161
#define av_log(a,...)
#define src
Definition: vp8dsp.c:254
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264dec.h:142
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2899
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition: h264dec.h:155
ThreadFrame tf
Definition: h264dec.h:130
simple assert() macros that are a bit more flexible than ISO C assert().
ThreadFrame * tf
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:149
#define fail()
Definition: checkasm.h:122
AVBufferRef * hwaccel_priv_buf
Definition: h264dec.h:141
useful rectangle filling function
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264_parse.h:49
AVBufferRef * motion_val_buf[2]
Definition: h264dec.h:135
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2891
int reference
Definition: h264dec.h:160
uint32_t * mb_type
Definition: h264dec.h:139
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Context Adaptive Binary Arithmetic Coder inline functions.
H.264 / AVC / MPEG-4 part10 codec.
AVCodecContext * avctx
Definition: h264dec.h:339
Libavcodec external API header.
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3829
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have add an so the codec calls ff_thread_report set AVCodecInternal allocate_progress The frames must then be freed with ff_thread_release_buffer().Otherwise leave it at zero and decode directly into the user-supplied frames.Call ff_thread_report_progress() after some part of the current picture has decoded.A good place to put this is where draw_horiz_band() is called-add this if it isn't called anywhere
int field_poc[2]
top/bottom POC
Definition: h264dec.h:147
main external API structure.
Definition: avcodec.h:1621
uint8_t * data
The data buffer.
Definition: buffer.h:89
int8_t * qscale_table
Definition: h264dec.h:133
int prev_frame_num
frame_num of the last pic for POC type 1/2
Definition: h264_parse.h:53
int8_t * ref_index[2]
Definition: h264dec.h:145
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:65
int mmco_reset
MMCO_RESET set this 1.
Definition: h264dec.h:150
H264Picture * cur_pic_ptr
Definition: h264dec.h:346
int8_t * ref_index[2]
int prev_poc_lsb
poc_lsb of the last reference pic for POC type 0
Definition: h264_parse.h:50
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264dec.h:157
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:153
common internal api header.
H.264 / AVC / MPEG-4 part10 motion vector prediction.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:94
AVFrame * f
int16_t(*[2] motion_val)[2]
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264dec.h:488
int ff_h264_execute_ref_pic_marking(H264Context *h)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:610
AVBufferRef * ref_index_buf[2]
Definition: h264dec.h:144
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264dec.h:156
int prev_frame_num_offset
for POC type 2
Definition: h264_parse.h:52
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:44
int droppable
Definition: h264dec.h:362
Context Adaptive Binary Arithmetic Coder.