avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / mpv_reconstruct_mb_template.c
blobdca982ae0fe6377b10a4e4450502423e6fa0fa29
1 /*
2 * MPEG macroblock reconstruction
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define NOT_MPEG12_H261 0
24 #define MAY_BE_MPEG12_H261 1
25 #define DEFINITELY_MPEG12_H261 2
27 /* put block[] to dest[] */
28 static inline void put_dct(MpegEncContext *s,
29 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
31 s->dct_unquantize_intra(s, block, i, qscale);
32 s->idsp.idct_put(dest, line_size, block);
35 static inline void add_dequant_dct(MpegEncContext *s,
36 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
38 if (s->block_last_index[i] >= 0) {
39 s->dct_unquantize_inter(s, block, i, qscale);
41 s->idsp.idct_add(dest, line_size, block);
45 /* generic function called after a macroblock has been parsed by the
46 decoder or after it has been encoded by the encoder.
48 Important variables used:
49 s->mb_intra : true if intra macroblock
50 s->mv_dir : motion vector direction
51 s->mv_type : motion vector type
52 s->mv : motion vector
53 s->interlaced_dct : true if interlaced dct used (mpeg2)
55 static av_always_inline
56 void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
57 int lowres_flag, int is_mpeg12)
59 #define IS_MPEG12_H261(s) (is_mpeg12 == MAY_BE_MPEG12_H261 ? ((s)->out_format <= FMT_H261) : is_mpeg12)
60 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
62 s->cur_pic.qscale_table[mb_xy] = s->qscale;
64 /* update DC predictors for P macroblocks */
65 if (!s->mb_intra) {
66 if (is_mpeg12 != DEFINITELY_MPEG12_H261 && (s->h263_pred || s->h263_aic)) {
67 if (s->mbintra_table[mb_xy])
68 ff_clean_intra_table_entries(s);
69 } else {
70 s->last_dc[0] =
71 s->last_dc[1] =
72 s->last_dc[2] = 128 << s->intra_dc_precision;
74 } else if (is_mpeg12 != DEFINITELY_MPEG12_H261 && (s->h263_pred || s->h263_aic))
75 s->mbintra_table[mb_xy] = 1;
77 #if IS_ENCODER
78 if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->frame_skip_threshold || s->frame_skip_factor ||
79 !((s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
80 s->avctx->mb_decision != FF_MB_DECISION_RD)) // FIXME precalc
81 #endif /* IS_ENCODER */
83 uint8_t *dest_y = s->dest[0], *dest_cb = s->dest[1], *dest_cr = s->dest[2];
84 int dct_linesize, dct_offset;
85 const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
86 const int uvlinesize = s->cur_pic.linesize[1];
87 const int block_size = lowres_flag ? 8 >> s->avctx->lowres : 8;
89 /* avoid copy if macroblock skipped in last frame too */
90 /* skip only during decoding as we might trash the buffers during encoding a bit */
91 if (!IS_ENCODER) {
92 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
94 if (s->mb_skipped) {
95 s->mb_skipped = 0;
96 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
97 *mbskip_ptr = 1;
98 } else if (!s->cur_pic.reference) {
99 *mbskip_ptr = 1;
100 } else{
101 *mbskip_ptr = 0; /* not skipped */
105 dct_linesize = linesize << s->interlaced_dct;
106 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
108 if (!s->mb_intra) {
109 /* motion handling */
110 /* decoding or more than one mb_type (MC was already done otherwise) */
112 #if !IS_ENCODER
113 if (HAVE_THREADS && is_mpeg12 != DEFINITELY_MPEG12_H261 &&
114 s->avctx->active_thread_type & FF_THREAD_FRAME) {
115 if (s->mv_dir & MV_DIR_FORWARD) {
116 ff_thread_progress_await(&s->last_pic.ptr->progress,
117 lowest_referenced_row(s, 0));
119 if (s->mv_dir & MV_DIR_BACKWARD) {
120 ff_thread_progress_await(&s->next_pic.ptr->progress,
121 lowest_referenced_row(s, 1));
125 if (lowres_flag) {
126 const h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
128 if (s->mv_dir & MV_DIR_FORWARD) {
129 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_pic.data, op_pix);
130 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
132 if (s->mv_dir & MV_DIR_BACKWARD) {
133 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_pic.data, op_pix);
135 } else {
136 const op_pixels_func (*op_pix)[4];
137 const qpel_mc_func (*op_qpix)[16];
139 if ((is_mpeg12 == DEFINITELY_MPEG12_H261 || !s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
140 op_pix = s->hdsp.put_pixels_tab;
141 op_qpix = s->qdsp.put_qpel_pixels_tab;
142 } else {
143 op_pix = s->hdsp.put_no_rnd_pixels_tab;
144 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
146 if (s->mv_dir & MV_DIR_FORWARD) {
147 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_pic.data, op_pix, op_qpix);
148 op_pix = s->hdsp.avg_pixels_tab;
149 op_qpix = s->qdsp.avg_qpel_pixels_tab;
151 if (s->mv_dir & MV_DIR_BACKWARD) {
152 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_pic.data, op_pix, op_qpix);
156 /* skip dequant / idct if we are really late ;) */
157 if (s->avctx->skip_idct) {
158 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
159 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
160 || s->avctx->skip_idct >= AVDISCARD_ALL)
161 return;
164 /* add dct residue */
165 if (!(IS_MPEG12_H261(s) || s->msmpeg4_version != MSMP4_UNUSED ||
166 (s->codec_id == AV_CODEC_ID_MPEG4 && !s->mpeg_quant)))
167 #endif /* !IS_ENCODER */
169 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
170 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
171 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
172 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
174 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
175 av_assert2(IS_ENCODER || s->chroma_y_shift);
176 if (!IS_ENCODER || s->chroma_y_shift) {
177 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
178 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
179 } else {
180 dct_linesize >>= 1;
181 dct_offset >>= 1;
182 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
183 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
184 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
185 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
189 #if !IS_ENCODER
190 else if (is_mpeg12 == DEFINITELY_MPEG12_H261 || lowres_flag || (s->codec_id != AV_CODEC_ID_WMV2)) {
191 add_dct(s, block[0], 0, dest_y , dct_linesize);
192 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
193 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
194 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
196 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
197 if (s->chroma_y_shift) {//Chroma420
198 add_dct(s, block[4], 4, dest_cb, uvlinesize);
199 add_dct(s, block[5], 5, dest_cr, uvlinesize);
200 } else {
201 //chroma422
202 dct_linesize = uvlinesize << s->interlaced_dct;
203 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
205 add_dct(s, block[4], 4, dest_cb, dct_linesize);
206 add_dct(s, block[5], 5, dest_cr, dct_linesize);
207 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
208 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
209 if (!s->chroma_x_shift) {//Chroma444
210 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
211 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
212 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
213 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
216 } //fi gray
217 } else if (CONFIG_WMV2_DECODER) {
218 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
220 #endif /* !IS_ENCODER */
221 } else {
222 #if !IS_ENCODER
223 /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
224 TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
225 if (is_mpeg12 != DEFINITELY_MPEG12_H261 && CONFIG_MPEG4_DECODER &&
226 /* s->codec_id == AV_CODEC_ID_MPEG4 && */
227 s->avctx->bits_per_raw_sample > 8) {
228 ff_mpeg4_decode_studio(s, dest_y, dest_cb, dest_cr, block_size,
229 uvlinesize, dct_linesize, dct_offset);
230 } else if (!IS_MPEG12_H261(s))
231 #endif /* !IS_ENCODER */
233 /* dct only in intra block */
234 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
235 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
236 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
237 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
239 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
240 if (s->chroma_y_shift) {
241 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
242 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
243 } else {
244 dct_offset >>=1;
245 dct_linesize >>=1;
246 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
247 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
248 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
249 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
253 #if !IS_ENCODER
254 else {
255 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
256 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
257 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
258 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
260 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
261 if (s->chroma_y_shift) {
262 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
263 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
264 } else {
265 dct_linesize = uvlinesize << s->interlaced_dct;
266 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
268 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
269 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
270 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
271 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
272 if (!s->chroma_x_shift) { //Chroma444
273 s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
274 s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
275 s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
276 s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
279 } //gray
281 #endif /* !IS_ENCODER */