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/emms.h"
30 #include "error_resilience.h"
31 #include "avcodec.h"
32 #include "h264dec.h"
33 #include "hwaccel_internal.h"
34 #include "mpegutils.h"
35 #include "refstruct.h"
36 #include "thread.h"
37 #include "threadframe.h"
38 
40 {
41  int off = offsetof(H264Picture, f_grain) + sizeof(pic->f_grain);
42  int i;
43 
44  if (!pic->f || !pic->f->buf[0])
45  return;
46 
48  av_frame_unref(pic->f_grain);
50 
53  ff_refstruct_unref(&pic->pps);
54  for (i = 0; i < 2; i++) {
57  }
59 
60  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
61 }
62 
64 {
65  ff_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
66  ff_refstruct_replace(&dst->mb_type_base, src->mb_type_base);
67  ff_refstruct_replace(&dst->pps, src->pps);
68 
69  for (int i = 0; i < 2; i++) {
70  ff_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
71  ff_refstruct_replace(&dst->ref_index[i], src->ref_index[i]);
72  }
73 
75  src->hwaccel_picture_private);
76 
77  ff_refstruct_replace(&dst->decode_error_flags, src->decode_error_flags);
78 
79  dst->qscale_table = src->qscale_table;
80  dst->mb_type = src->mb_type;
81 
82  for (int i = 0; i < 2; i++)
83  dst->motion_val[i] = src->motion_val[i];
84 
85  for (int i = 0; i < 2; i++)
86  dst->field_poc[i] = src->field_poc[i];
87 
88  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
89  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
90 
91  dst->poc = src->poc;
92  dst->frame_num = src->frame_num;
93  dst->mmco_reset = src->mmco_reset;
94  dst->long_ref = src->long_ref;
95  dst->mbaff = src->mbaff;
96  dst->field_picture = src->field_picture;
97  dst->reference = src->reference;
98  dst->recovered = src->recovered;
99  dst->gray = src->gray;
100  dst->invalid_gap = src->invalid_gap;
101  dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
102  dst->mb_width = src->mb_width;
103  dst->mb_height = src->mb_height;
104  dst->mb_stride = src->mb_stride;
105  dst->needs_fg = src->needs_fg;
106 }
107 
109 {
110  int ret;
111 
112  av_assert0(!dst->f->buf[0]);
113  av_assert0(src->f->buf[0]);
114  av_assert0(src->tf.f == src->f);
115 
116  dst->tf.f = dst->f;
117  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
118  if (ret < 0)
119  goto fail;
120 
121  if (src->needs_fg) {
122  ret = av_frame_ref(dst->f_grain, src->f_grain);
123  if (ret < 0)
124  goto fail;
125  }
126 
128 
129  return 0;
130 fail:
132  return ret;
133 }
134 
136 {
137  int ret;
138 
139  if (!src->f || !src->f->buf[0]) {
141  return 0;
142  }
143 
144  av_assert0(src->tf.f == src->f);
145 
146  dst->tf.f = dst->f;
147  ret = ff_thread_replace_frame(&dst->tf, &src->tf);
148  if (ret < 0)
149  goto fail;
150 
151  if (src->needs_fg) {
152  av_frame_unref(dst->f_grain);
153  ret = av_frame_ref(dst->f_grain, src->f_grain);
154  if (ret < 0)
155  goto fail;
156  }
157 
159 
160  return 0;
161 fail:
163  return ret;
164 }
165 
167 {
168 #if CONFIG_ERROR_RESILIENCE
169  int i;
170 
171  memset(dst, 0, sizeof(*dst));
172 
173  if (!src)
174  return;
175 
176  dst->f = src->f;
177  dst->tf = &src->tf;
178 
179  for (i = 0; i < 2; i++) {
180  dst->motion_val[i] = src->motion_val[i];
181  dst->ref_index[i] = src->ref_index[i];
182  }
183 
184  dst->mb_type = src->mb_type;
185  dst->field_picture = src->field_picture;
186 #endif /* CONFIG_ERROR_RESILIENCE */
187 }
188 
190 {
191  AVCodecContext *const avctx = h->avctx;
192  H264Picture *cur = h->cur_pic_ptr;
193  int err = 0;
194  h->mb_y = 0;
195 
196  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
197  if (!h->droppable) {
199  h->poc.prev_poc_msb = h->poc.poc_msb;
200  h->poc.prev_poc_lsb = h->poc.poc_lsb;
201  }
202  h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
203  h->poc.prev_frame_num = h->poc.frame_num;
204  }
205 
206  if (avctx->hwaccel) {
207  err = FF_HW_SIMPLE_CALL(avctx, end_frame);
208  if (err < 0)
209  av_log(avctx, AV_LOG_ERROR,
210  "hardware accelerator failed to decode picture\n");
211  } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) {
213 
214  err = AVERROR_INVALIDDATA;
215  if (sd) // a decoding error may have happened before the side data could be allocated
216  err = ff_h274_apply_film_grain(cur->f_grain, cur->f, &h->h274db,
217  (AVFilmGrainParams *) sd->data);
218  if (err < 0) {
219  av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film "
220  "grain, ignoring: %s\n", av_err2str(err));
221  cur->needs_fg = 0;
222  err = 0;
223  }
224  }
225 
226  if (!in_setup && !h->droppable)
227  ff_thread_report_progress(&cur->tf, INT_MAX,
228  h->picture_structure == PICT_BOTTOM_FIELD);
229  emms_c();
230 
231  h->current_slice = 0;
232 
233  return err;
234 }
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1439
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
H264Picture::poc
int poc
frame POC
Definition: h264dec.h:127
H264Picture::f
AVFrame * f
Definition: h264dec.h:107
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:824
ff_h264_ref_picture
int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src)
Definition: h264_picture.c:108
H264Picture::ref_count
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264dec.h:135
H264Picture::ref_index
int8_t * ref_index[2]
RefStruct reference.
Definition: h264dec.h:124
ERPicture::ref_index
int8_t * ref_index[2]
Definition: error_resilience.h:46
AV_FRAME_DATA_FILM_GRAIN_PARAMS
@ AV_FRAME_DATA_FILM_GRAIN_PARAMS
Film grain parameters for a frame, described by AVFilmGrainParams.
Definition: frame.h:184
H264Picture::pps
const PPS * pps
Definition: h264dec.h:150
H264Picture::qscale_table
int8_t * qscale_table
Definition: h264dec.h:113
PICT_BOTTOM_FIELD
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:37
FF_HW_SIMPLE_CALL
#define FF_HW_SIMPLE_CALL(avctx, function)
Definition: hwaccel_internal.h:174
mpegutils.h
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
H264Picture::invalid_gap
int invalid_gap
Definition: h264dec.h:146
thread.h
ThreadFrame::f
AVFrame * f
Definition: threadframe.h:28
ERPicture
Definition: error_resilience.h:40
H264Picture::frame_num
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:128
H264SliceContext
Definition: h264dec.h:172
H264Picture::mmco_reset
int mmco_reset
MMCO_RESET set this 1.
Definition: h264dec.h:129
fail
#define fail()
Definition: checkasm.h:179
H264Picture::ref_poc
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition: h264dec.h:134
ERPicture::motion_val
int16_t(*[2] motion_val)[2]
Definition: error_resilience.h:45
ff_thread_replace_frame
int ff_thread_replace_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:890
H264Picture::mb_stride
int mb_stride
Definition: h264dec.h:153
H264Picture::f_grain
AVFrame * f_grain
Definition: h264dec.h:110
refstruct.h
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:589
H264Picture::mbaff
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264dec.h:136
emms_c
#define emms_c()
Definition: emms.h:63
H264Picture::qscale_table_base
int8_t * qscale_table_base
RefStruct reference.
Definition: h264dec.h:112
ERPicture::field_picture
int field_picture
Definition: error_resilience.h:49
FIELD_PICTURE
#define FIELD_PICTURE(h)
Definition: h264dec.h:67
ff_h264_execute_ref_pic_marking
int ff_h264_execute_ref_pic_marking(H264Context *h)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:621
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ERPicture::f
AVFrame * f
Definition: error_resilience.h:41
H264Picture::sei_recovery_frame_cnt
int sei_recovery_frame_cnt
Definition: h264dec.h:147
ERPicture::mb_type
uint32_t * mb_type
Definition: error_resilience.h:48
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:871
threadframe.h
H264Picture::mb_type_base
uint32_t * mb_type_base
RefStruct reference.
Definition: h264dec.h:118
hwaccel_internal.h
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:1012
H264Picture::mb_height
int mb_height
Definition: h264dec.h:152
ERPicture::tf
const struct ThreadFrame * tf
Definition: error_resilience.h:42
H264Picture::reference
int reference
Definition: h264dec.h:144
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:361
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
H264Picture::tf
ThreadFrame tf
Definition: h264dec.h:108
H264Picture::mb_type
uint32_t * mb_type
Definition: h264dec.h:119
H264Picture::decode_error_flags
atomic_int * decode_error_flags
RefStruct reference; its pointee is shared between decoding threads.
Definition: h264dec.h:156
H264Picture::recovered
int recovered
picture at IDR or recovery point + recovery count
Definition: h264dec.h:145
H264Picture::gray
int gray
Definition: h264dec.h:158
AVFrameSideData::data
uint8_t * data
Definition: frame.h:248
AVFilmGrainParams
This structure describes how to handle film grain synthesis in video for specific codecs.
Definition: film_grain_params.h:216
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1547
emms.h
H264Picture::field_picture
int field_picture
whether or not picture was encoded in separate fields
Definition: h264dec.h:137
h264dec.h
ff_h274_apply_film_grain
int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, H274FilmGrainDatabase *database, const AVFilmGrainParams *params)
Definition: h274.c:217
H264Picture::needs_fg
int needs_fg
whether picture needs film grain synthesis (see f_grain)
Definition: h264dec.h:148
H264Context
H264Context.
Definition: h264dec.h:332
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
ff_h264_replace_picture
int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src)
Definition: h264_picture.c:135
h264_copy_picture_params
static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
Definition: h264_picture.c:63
avcodec.h
ret
ret
Definition: filter_design.txt:187
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1555
H264Picture::motion_val_base
int16_t(*[2] motion_val_base)[2]
RefStruct reference.
Definition: h264dec.h:115
H264Picture::field_poc
int field_poc[2]
top/bottom POC
Definition: h264dec.h:126
error_resilience.h
H264Picture::mb_width
int mb_width
Definition: h264dec.h:152
ff_h264_unref_picture
void ff_h264_unref_picture(H264Picture *pic)
Definition: h264_picture.c:39
H264Picture
Definition: h264dec.h:106
ff_h264_field_end
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:189
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:246
H264Picture::hwaccel_picture_private
void * hwaccel_picture_private
RefStruct reference for hardware accelerator private data.
Definition: h264dec.h:122
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
H264Picture::long_ref
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:133
H264Picture::motion_val
int16_t(*[2] motion_val)[2]
Definition: h264dec.h:116
ff_h264_set_erpic
void ff_h264_set_erpic(ERPicture *dst, const H264Picture *src)
Definition: h264_picture.c:166