Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * MPEG-1/2 encoder | ||
3 | * Copyright (c) 2000,2001 Fabrice Bellard | ||
4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
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. | ||
12 | * | ||
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. | ||
17 | * | ||
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 | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file | ||
25 | * MPEG-1/2 encoder | ||
26 | */ | ||
27 | |||
28 | #include <stdint.h> | ||
29 | |||
30 | #include "config.h" | ||
31 | #include "config_components.h" | ||
32 | #include "libavutil/attributes.h" | ||
33 | #include "libavutil/avassert.h" | ||
34 | #include "libavutil/log.h" | ||
35 | #include "libavutil/opt.h" | ||
36 | #include "libavutil/thread.h" | ||
37 | #include "libavutil/timecode.h" | ||
38 | #include "libavutil/stereo3d.h" | ||
39 | |||
40 | #include "avcodec.h" | ||
41 | #include "codec_internal.h" | ||
42 | #include "mathops.h" | ||
43 | #include "mpeg12.h" | ||
44 | #include "mpeg12data.h" | ||
45 | #include "mpeg12enc.h" | ||
46 | #include "mpeg12vlc.h" | ||
47 | #include "mpegutils.h" | ||
48 | #include "mpegvideo.h" | ||
49 | #include "mpegvideodata.h" | ||
50 | #include "mpegvideoenc.h" | ||
51 | #include "profiles.h" | ||
52 | #include "rl.h" | ||
53 | |||
54 | #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER | ||
55 | static const uint8_t svcd_scan_offset_placeholder[] = { | ||
56 | 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80, | ||
57 | 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | ||
58 | }; | ||
59 | |||
60 | static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1]; | ||
61 | static uint8_t fcode_tab[MAX_MV * 2 + 1]; | ||
62 | |||
63 | static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2]; | ||
64 | static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2]; | ||
65 | |||
66 | static uint8_t mpeg12_max_level[MAX_LEVEL + 1]; | ||
67 | static uint8_t mpeg12_index_run[MAX_RUN + 1]; | ||
68 | |||
69 | /* simple include everything table for dc, first byte is bits | ||
70 | * number next 3 are code */ | ||
71 | static uint32_t mpeg1_lum_dc_uni[512]; | ||
72 | static uint32_t mpeg1_chr_dc_uni[512]; | ||
73 | |||
74 | typedef struct MPEG12EncContext { | ||
75 | MpegEncContext mpeg; | ||
76 | AVRational frame_rate_ext; | ||
77 | unsigned frame_rate_index; | ||
78 | |||
79 | int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num | ||
80 | |||
81 | int64_t timecode_frame_start; ///< GOP timecode frame start number, in non drop frame format | ||
82 | AVTimecode tc; ///< timecode context | ||
83 | char *tc_opt_str; ///< timecode option string | ||
84 | int drop_frame_timecode; ///< timecode is in drop frame format. | ||
85 | int scan_offset; ///< reserve space for SVCD scan offset user data. | ||
86 | |||
87 | int a53_cc; | ||
88 | int seq_disp_ext; | ||
89 | int video_format; | ||
90 | #define VIDEO_FORMAT_COMPONENT 0 | ||
91 | #define VIDEO_FORMAT_PAL 1 | ||
92 | #define VIDEO_FORMAT_NTSC 2 | ||
93 | #define VIDEO_FORMAT_SECAM 3 | ||
94 | #define VIDEO_FORMAT_MAC 4 | ||
95 | #define VIDEO_FORMAT_UNSPECIFIED 5 | ||
96 | } MPEG12EncContext; | ||
97 | |||
98 | #define A53_MAX_CC_COUNT 0x1f | ||
99 | #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */ | ||
100 | |||
101 | 119 | av_cold void ff_mpeg1_init_uni_ac_vlc(const int8_t max_level[], | |
102 | const uint8_t index_run[], | ||
103 | const uint16_t table_vlc[][2], | ||
104 | uint8_t uni_ac_vlc_len[]) | ||
105 | { | ||
106 | int i; | ||
107 | |||
108 |
2/2✓ Branch 0 taken 15232 times.
✓ Branch 1 taken 119 times.
|
15351 | for (i = 0; i < 128; i++) { |
109 | 15232 | int level = i - 64; | |
110 | int run; | ||
111 |
2/2✓ Branch 0 taken 119 times.
✓ Branch 1 taken 15113 times.
|
15232 | if (!level) |
112 | 119 | continue; | |
113 |
2/2✓ Branch 0 taken 967232 times.
✓ Branch 1 taken 15113 times.
|
982345 | for (run = 0; run < 64; run++) { |
114 | int len, code; | ||
115 | 967232 | int alevel = FFABS(level); | |
116 | |||
117 |
2/2✓ Branch 0 taken 940634 times.
✓ Branch 1 taken 26598 times.
|
967232 | if (alevel > max_level[run]) |
118 | 940634 | code = 111; /* rl->n */ | |
119 | else | ||
120 | 26598 | code = index_run[run] + alevel - 1; | |
121 | |||
122 |
2/2✓ Branch 0 taken 26418 times.
✓ Branch 1 taken 940814 times.
|
967232 | if (code < 111) { /* rl->n */ |
123 | /* length of VLC and sign */ | ||
124 | 26418 | len = table_vlc[code][1] + 1; | |
125 | } else { | ||
126 | 940814 | len = table_vlc[MPEG12_RL_NB_ELEMS][1] + 6; | |
127 | |||
128 |
1/2✓ Branch 0 taken 940814 times.
✗ Branch 1 not taken.
|
940814 | if (alevel < 128) |
129 | 940814 | len += 8; | |
130 | else | ||
131 | ✗ | len += 16; | |
132 | } | ||
133 | |||
134 | 967232 | uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len; | |
135 | } | ||
136 | } | ||
137 | 119 | } | |
138 | |||
139 | #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER | ||
140 | 55 | static int find_frame_rate_index(AVCodecContext *avctx, MPEG12EncContext *mpeg12) | |
141 | { | ||
142 | int i; | ||
143 | 55 | AVRational bestq = (AVRational) {0, 0}; | |
144 | AVRational ext; | ||
145 | 55 | AVRational target = av_inv_q(avctx->time_base); | |
146 | |||
147 |
2/2✓ Branch 0 taken 499 times.
✓ Branch 1 taken 1 times.
|
500 | for (i = 1; i < 14; i++) { |
148 |
4/4✓ Branch 0 taken 486 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 432 times.
|
499 | if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && |
149 | i >= 9) | ||
150 | 54 | break; | |
151 | |||
152 |
2/2✓ Branch 0 taken 1780 times.
✓ Branch 1 taken 445 times.
|
2225 | for (ext.num=1; ext.num <= 4; ext.num++) { |
153 |
2/2✓ Branch 0 taken 56960 times.
✓ Branch 1 taken 1780 times.
|
58740 | for (ext.den=1; ext.den <= 32; ext.den++) { |
154 | 56960 | AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]); | |
155 | |||
156 |
6/6✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 45696 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 10912 times.
✓ Branch 4 taken 264 times.
✓ Branch 5 taken 88 times.
|
56960 | if (avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1)) |
157 | 26170 | continue; | |
158 |
2/2✓ Branch 0 taken 14994 times.
✓ Branch 1 taken 30790 times.
|
45784 | if (av_gcd(ext.den, ext.num) != 1) |
159 | 14994 | continue; | |
160 | |||
161 |
2/2✓ Branch 0 taken 30735 times.
✓ Branch 1 taken 55 times.
|
30790 | if ( bestq.num==0 |
162 |
2/2✓ Branch 1 taken 30597 times.
✓ Branch 2 taken 138 times.
|
30735 | || av_nearer_q(target, bestq, q) < 0 |
163 |
5/6✓ Branch 0 taken 11329 times.
✓ Branch 1 taken 19268 times.
✓ Branch 2 taken 286 times.
✓ Branch 3 taken 11043 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 286 times.
|
30597 | || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) { |
164 | 193 | bestq = q; | |
165 | 193 | mpeg12->frame_rate_index = i; | |
166 | 193 | mpeg12->frame_rate_ext.num = ext.num; | |
167 | 193 | mpeg12->frame_rate_ext.den = ext.den; | |
168 | } | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | |||
173 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 54 times.
|
55 | if (av_cmp_q(target, bestq)) |
174 | 1 | return -1; | |
175 | else | ||
176 | 54 | return 0; | |
177 | } | ||
178 | |||
179 | 55 | static av_cold int encode_init(AVCodecContext *avctx) | |
180 | { | ||
181 | 55 | MPEG12EncContext *const mpeg12 = avctx->priv_data; | |
182 | int ret; | ||
183 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 11 times.
|
55 | int max_size = avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 16383 : 4095; |
184 | |||
185 |
2/4✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
|
55 | if (avctx->width > max_size || avctx->height > max_size) { |
186 | ✗ | av_log(avctx, AV_LOG_ERROR, "%s does not support resolutions above %dx%d\n", | |
187 | ✗ | CONFIG_SMALL ? avctx->codec->name : avctx->codec->long_name, | |
188 | max_size, max_size); | ||
189 | ✗ | return AVERROR(EINVAL); | |
190 | } | ||
191 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
55 | if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) { |
192 | ✗ | av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n"); | |
193 | ✗ | return AVERROR(EINVAL); | |
194 | } | ||
195 | |||
196 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1 times.
|
55 | if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { |
197 |
2/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
|
54 | if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) { |
198 | ✗ | av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n" | |
199 | "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL); | ||
200 | ✗ | return AVERROR(EINVAL); | |
201 | } | ||
202 | } | ||
203 | |||
204 |
1/2✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 | if (avctx->profile == AV_PROFILE_UNKNOWN) { |
205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
|
55 | if (avctx->level != AV_LEVEL_UNKNOWN) { |
206 | ✗ | av_log(avctx, AV_LOG_ERROR, "Set profile and level\n"); | |
207 | ✗ | return AVERROR(EINVAL); | |
208 | } | ||
209 | /* Main or 4:2:2 */ | ||
210 | 55 | avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? AV_PROFILE_MPEG2_MAIN | |
211 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
|
55 | : AV_PROFILE_MPEG2_422; |
212 | } | ||
213 |
1/2✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 | if (avctx->level == AV_LEVEL_UNKNOWN) { |
214 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 50 times.
|
55 | if (avctx->profile == AV_PROFILE_MPEG2_422) { /* 4:2:2 */ |
215 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (avctx->width <= 720 && avctx->height <= 608) |
216 | 5 | avctx->level = 5; /* Main */ | |
217 | else | ||
218 | ✗ | avctx->level = 2; /* High */ | |
219 | } else { | ||
220 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | if (avctx->profile != AV_PROFILE_MPEG2_HIGH && |
221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | avctx->pix_fmt != AV_PIX_FMT_YUV420P) { |
222 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
223 | "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n"); | ||
224 | ✗ | return AVERROR(EINVAL); | |
225 | } | ||
226 |
3/4✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
|
50 | if (avctx->width <= 720 && avctx->height <= 576) |
227 | 49 | avctx->level = 8; /* Main */ | |
228 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | else if (avctx->width <= 1440) |
229 | 1 | avctx->level = 6; /* High 1440 */ | |
230 | else | ||
231 | ✗ | avctx->level = 4; /* High */ | |
232 | } | ||
233 | } | ||
234 | |||
235 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
|
55 | if ((ret = ff_mpv_encode_init(avctx)) < 0) |
236 | ✗ | return ret; | |
237 | |||
238 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 54 times.
|
55 | if (find_frame_rate_index(avctx, mpeg12) < 0) { |
239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { |
240 | ✗ | av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n", | |
241 | avctx->time_base.den, avctx->time_base.num); | ||
242 | ✗ | return AVERROR(EINVAL); | |
243 | } else { | ||
244 | 1 | av_log(avctx, AV_LOG_INFO, | |
245 | "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n", | ||
246 | avctx->time_base.den, avctx->time_base.num); | ||
247 | } | ||
248 | } | ||
249 | |||
250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
|
55 | if (mpeg12->drop_frame_timecode) |
251 | ✗ | mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME; | |
252 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
55 | if (mpeg12->drop_frame_timecode && mpeg12->frame_rate_index != 4) { |
253 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
254 | "Drop frame time code only allowed with 1001/30000 fps\n"); | ||
255 | ✗ | return AVERROR(EINVAL); | |
256 | } | ||
257 | |||
258 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 49 times.
|
55 | if (mpeg12->tc_opt_str) { |
259 | 6 | AVRational rate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index]; | |
260 | 6 | int ret = av_timecode_init_from_string(&mpeg12->tc, rate, mpeg12->tc_opt_str, avctx); | |
261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
262 | ✗ | return ret; | |
263 | 6 | mpeg12->drop_frame_timecode = !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME); | |
264 | 6 | mpeg12->timecode_frame_start = mpeg12->tc.start; | |
265 | } else { | ||
266 | 49 | mpeg12->timecode_frame_start = 0; // default is -1 | |
267 | } | ||
268 | |||
269 | 55 | return 0; | |
270 | } | ||
271 | |||
272 | 307216 | static void put_header(MpegEncContext *s, uint32_t header) | |
273 | { | ||
274 | 307216 | align_put_bits(&s->pb); | |
275 | 307216 | put_bits32(&s->pb, header); | |
276 | 307216 | } | |
277 | |||
278 | /* put sequence header if needed */ | ||
279 | 3576 | static void mpeg1_encode_sequence_header(MpegEncContext *s) | |
280 | { | ||
281 | 3576 | MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s; | |
282 | unsigned int vbv_buffer_size, fps, v; | ||
283 | int constraint_parameter_flag; | ||
284 | 3576 | AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index]; | |
285 | uint64_t time_code; | ||
286 | 3576 | int64_t best_aspect_error = INT64_MAX; | |
287 | 3576 | AVRational aspect_ratio = s->avctx->sample_aspect_ratio; | |
288 | int aspect_ratio_info; | ||
289 | |||
290 |
2/2✓ Branch 0 taken 3162 times.
✓ Branch 1 taken 414 times.
|
3576 | if (!(s->cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)) |
291 | 3162 | return; | |
292 | |||
293 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 406 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
414 | if (aspect_ratio.num == 0 || aspect_ratio.den == 0) |
294 | 406 | aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA) | |
295 | |||
296 | /* MPEG-1 header repeated every GOP */ | ||
297 | 414 | put_header(s, SEQ_START_CODE); | |
298 | |||
299 | 414 | put_sbits(&s->pb, 12, s->width & 0xFFF); | |
300 | 414 | put_sbits(&s->pb, 12, s->height & 0xFFF); | |
301 | |||
302 |
2/2✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 414 times.
|
6210 | for (int i = 1; i < 15; i++) { |
303 | 5796 | int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den; | |
304 |
4/4✓ Branch 0 taken 5054 times.
✓ Branch 1 taken 742 times.
✓ Branch 2 taken 361 times.
✓ Branch 3 taken 4693 times.
|
5796 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1) |
305 | 1103 | error -= (1LL<<32) / ff_mpeg1_aspect[i]; | |
306 | else | ||
307 | 4693 | error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->height / s->width / ff_mpeg2_aspect[i].den; | |
308 | |||
309 | 5796 | error = FFABS(error); | |
310 | |||
311 |
2/2✓ Branch 0 taken 417 times.
✓ Branch 1 taken 5379 times.
|
5796 | if (error - 2 <= best_aspect_error) { |
312 | 417 | best_aspect_error = error; | |
313 | 417 | aspect_ratio_info = i; | |
314 | } | ||
315 | } | ||
316 | |||
317 | 414 | put_bits(&s->pb, 4, aspect_ratio_info); | |
318 | 414 | put_bits(&s->pb, 4, mpeg12->frame_rate_index); | |
319 | |||
320 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 261 times.
|
414 | if (s->avctx->rc_max_rate) { |
321 | 153 | v = (s->avctx->rc_max_rate + 399) / 400; | |
322 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
153 | if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
323 | ✗ | v = 0x3ffff; | |
324 | } else { | ||
325 | 261 | v = 0x3FFFF; | |
326 | } | ||
327 | |||
328 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 261 times.
|
414 | if (s->avctx->rc_buffer_size) |
329 | 153 | vbv_buffer_size = s->avctx->rc_buffer_size; | |
330 | else | ||
331 | /* VBV calculation: Scaled so that a VCD has the proper | ||
332 | * VBV size of 40 kilobytes */ | ||
333 | 261 | vbv_buffer_size = av_rescale_rnd(s->bit_rate, 20, 1151929 / 2, AV_ROUND_ZERO) * 8 * 1024; | |
334 | 414 | vbv_buffer_size = (vbv_buffer_size + 16383) / 16384; | |
335 | |||
336 | 414 | put_sbits(&s->pb, 18, v); | |
337 | 414 | put_bits(&s->pb, 1, 1); // marker | |
338 | 414 | put_sbits(&s->pb, 10, vbv_buffer_size); | |
339 | |||
340 | 414 | constraint_parameter_flag = | |
341 | 825 | s->width <= 768 && | |
342 |
2/2✓ Branch 0 taken 261 times.
✓ Branch 1 taken 150 times.
|
411 | s->height <= 576 && |
343 |
2/2✓ Branch 0 taken 238 times.
✓ Branch 1 taken 23 times.
|
261 | s->mb_width * s->mb_height <= 396 && |
344 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 6 times.
|
238 | s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den && |
345 |
1/2✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
|
232 | framerate.num <= framerate.den * 30 && |
346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | s->avctx->me_range && |
347 | ✗ | s->avctx->me_range < 128 && | |
348 | ✗ | vbv_buffer_size <= 20 && | |
349 |
2/2✓ Branch 0 taken 411 times.
✓ Branch 1 taken 3 times.
|
825 | v <= 1856000 / 400 && |
350 | ✗ | s->codec_id == AV_CODEC_ID_MPEG1VIDEO; | |
351 | |||
352 | 414 | put_bits(&s->pb, 1, constraint_parameter_flag); | |
353 | |||
354 | 414 | ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix); | |
355 | 414 | ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix); | |
356 | |||
357 |
2/2✓ Branch 0 taken 361 times.
✓ Branch 1 taken 53 times.
|
414 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
358 | const AVFrameSideData *side_data; | ||
359 | 361 | int width = s->width; | |
360 | 361 | int height = s->height; | |
361 | int use_seq_disp_ext; | ||
362 | |||
363 | 361 | put_header(s, EXT_START_CODE); | |
364 | 361 | put_bits(&s->pb, 4, 1); // seq ext | |
365 | |||
366 | 361 | put_bits(&s->pb, 1, s->avctx->profile == AV_PROFILE_MPEG2_422); // escx 1 for 4:2:2 profile | |
367 | |||
368 | 361 | put_bits(&s->pb, 3, s->avctx->profile); // profile | |
369 | 361 | put_bits(&s->pb, 4, s->avctx->level); // level | |
370 | |||
371 | 361 | put_bits(&s->pb, 1, s->progressive_sequence); | |
372 | 361 | put_bits(&s->pb, 2, s->chroma_format); | |
373 | 361 | put_bits(&s->pb, 2, s->width >> 12); | |
374 | 361 | put_bits(&s->pb, 2, s->height >> 12); | |
375 | 361 | put_bits(&s->pb, 12, v >> 18); // bitrate ext | |
376 | 361 | put_bits(&s->pb, 1, 1); // marker | |
377 | 361 | put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext | |
378 | 361 | put_bits(&s->pb, 1, s->low_delay); | |
379 | 361 | put_bits(&s->pb, 2, mpeg12->frame_rate_ext.num-1); // frame_rate_ext_n | |
380 | 361 | put_bits(&s->pb, 5, mpeg12->frame_rate_ext.den-1); // frame_rate_ext_d | |
381 | |||
382 | 361 | side_data = av_frame_get_side_data(s->cur_pic.ptr->f, AV_FRAME_DATA_PANSCAN); | |
383 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 356 times.
|
361 | if (side_data) { |
384 | 5 | const AVPanScan *pan_scan = (AVPanScan *)side_data->data; | |
385 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (pan_scan->width && pan_scan->height) { |
386 | 5 | width = pan_scan->width >> 4; | |
387 | 5 | height = pan_scan->height >> 4; | |
388 | } | ||
389 | } | ||
390 | |||
391 | 1083 | use_seq_disp_ext = (width != s->width || | |
392 |
1/2✓ Branch 0 taken 361 times.
✗ Branch 1 not taken.
|
361 | height != s->height || |
393 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 9 times.
|
361 | s->avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || |
394 |
2/2✓ Branch 0 taken 349 times.
✓ Branch 1 taken 3 times.
|
352 | s->avctx->color_trc != AVCOL_TRC_UNSPECIFIED || |
395 |
2/4✓ Branch 0 taken 361 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 349 times.
✗ Branch 3 not taken.
|
1071 | s->avctx->colorspace != AVCOL_SPC_UNSPECIFIED || |
396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 349 times.
|
349 | mpeg12->video_format != VIDEO_FORMAT_UNSPECIFIED); |
397 | |||
398 |
1/2✓ Branch 0 taken 361 times.
✗ Branch 1 not taken.
|
361 | if (mpeg12->seq_disp_ext == 1 || |
399 |
3/4✓ Branch 0 taken 361 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 349 times.
|
361 | (mpeg12->seq_disp_ext == -1 && use_seq_disp_ext)) { |
400 | 12 | put_header(s, EXT_START_CODE); | |
401 | 12 | put_bits(&s->pb, 4, 2); // sequence display extension | |
402 | 12 | put_bits(&s->pb, 3, mpeg12->video_format); // video_format | |
403 | 12 | put_bits(&s->pb, 1, 1); // colour_description | |
404 | 12 | put_bits(&s->pb, 8, s->avctx->color_primaries); // colour_primaries | |
405 | 12 | put_bits(&s->pb, 8, s->avctx->color_trc); // transfer_characteristics | |
406 | 12 | put_bits(&s->pb, 8, s->avctx->colorspace); // matrix_coefficients | |
407 | 12 | put_bits(&s->pb, 14, width); // display_horizontal_size | |
408 | 12 | put_bits(&s->pb, 1, 1); // marker_bit | |
409 | 12 | put_bits(&s->pb, 14, height); // display_vertical_size | |
410 | 12 | put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding | |
411 | } | ||
412 | } | ||
413 | |||
414 | 414 | put_header(s, GOP_START_CODE); | |
415 | 414 | put_bits(&s->pb, 1, mpeg12->drop_frame_timecode); // drop frame flag | |
416 | /* time code: we must convert from the real frame rate to a | ||
417 | * fake MPEG frame rate in case of low frame rate */ | ||
418 | 414 | fps = (framerate.num + framerate.den / 2) / framerate.den; | |
419 | 414 | time_code = s->cur_pic.ptr->coded_picture_number + | |
420 | 414 | mpeg12->timecode_frame_start; | |
421 | |||
422 | 414 | mpeg12->gop_picture_number = s->cur_pic.ptr->coded_picture_number; | |
423 | |||
424 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
|
414 | av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME)); |
425 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 405 times.
|
414 | if (mpeg12->drop_frame_timecode) |
426 | 9 | time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps); | |
427 | |||
428 | 414 | put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); | |
429 | 414 | put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); | |
430 | 414 | put_bits(&s->pb, 1, 1); | |
431 | 414 | put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60)); | |
432 | 414 | put_bits(&s->pb, 6, (uint32_t)((time_code % fps))); | |
433 | 414 | put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) || | |
434 |
5/6✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 264 times.
✓ Branch 3 taken 150 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 210 times.
|
414 | s->intra_only || !mpeg12->gop_picture_number); |
435 | 414 | put_bits(&s->pb, 1, 0); // broken link | |
436 | } | ||
437 | |||
438 | 1776803 | static inline void encode_mb_skip_run(MpegEncContext *s, int run) | |
439 | { | ||
440 |
2/2✓ Branch 0 taken 69199 times.
✓ Branch 1 taken 1776803 times.
|
1846002 | while (run >= 33) { |
441 | 69199 | put_bits(&s->pb, 11, 0x008); | |
442 | 69199 | run -= 33; | |
443 | } | ||
444 | 1776803 | put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1], | |
445 | 1776803 | ff_mpeg12_mbAddrIncrTable[run][0]); | |
446 | 1776803 | } | |
447 | |||
448 | 481367 | static av_always_inline void put_qscale(MpegEncContext *s) | |
449 | { | ||
450 | 481367 | put_bits(&s->pb, 5, s->qscale); | |
451 | 481367 | } | |
452 | |||
453 | 299209 | void ff_mpeg1_encode_slice_header(MpegEncContext *s) | |
454 | { | ||
455 |
3/4✓ Branch 0 taken 291936 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 291936 times.
|
299209 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) { |
456 | ✗ | put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127)); | |
457 | /* slice_vertical_position_extension */ | ||
458 | ✗ | put_bits(&s->pb, 3, s->mb_y >> 7); | |
459 | } else { | ||
460 | 299209 | put_header(s, SLICE_MIN_START_CODE + s->mb_y); | |
461 | } | ||
462 | 299209 | put_qscale(s); | |
463 | /* slice extra information */ | ||
464 | 299209 | put_bits(&s->pb, 1, 0); | |
465 | 299209 | } | |
466 | |||
467 | 3576 | void ff_mpeg1_encode_picture_header(MpegEncContext *s) | |
468 | { | ||
469 | 3576 | MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s; | |
470 | const AVFrameSideData *side_data; | ||
471 | 3576 | mpeg1_encode_sequence_header(s); | |
472 | |||
473 | /* MPEG-1 picture header */ | ||
474 | 3576 | put_header(s, PICTURE_START_CODE); | |
475 | /* temporal reference */ | ||
476 | |||
477 | // RAL: s->picture_number instead of s->fake_picture_number | ||
478 | 3576 | put_bits(&s->pb, 10, | |
479 | 3576 | (s->picture_number - mpeg12->gop_picture_number) & 0x3ff); | |
480 | 3576 | put_bits(&s->pb, 3, s->pict_type); | |
481 | |||
482 | 3576 | s->vbv_delay_pos = put_bytes_count(&s->pb, 0); | |
483 | 3576 | put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */ | |
484 | |||
485 | // RAL: Forward f_code also needed for B-frames | ||
486 |
2/2✓ Branch 0 taken 1121 times.
✓ Branch 1 taken 2455 times.
|
3576 | if (s->pict_type == AV_PICTURE_TYPE_P || |
487 |
2/2✓ Branch 0 taken 707 times.
✓ Branch 1 taken 414 times.
|
1121 | s->pict_type == AV_PICTURE_TYPE_B) { |
488 | 3162 | put_bits(&s->pb, 1, 0); /* half pel coordinates */ | |
489 |
2/2✓ Branch 0 taken 427 times.
✓ Branch 1 taken 2735 times.
|
3162 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
490 | 427 | put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ | |
491 | else | ||
492 | 2735 | put_bits(&s->pb, 3, 7); /* forward_f_code */ | |
493 | } | ||
494 | |||
495 | // RAL: Backward f_code necessary for B-frames | ||
496 |
2/2✓ Branch 0 taken 707 times.
✓ Branch 1 taken 2869 times.
|
3576 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
497 | 707 | put_bits(&s->pb, 1, 0); /* half pel coordinates */ | |
498 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 563 times.
|
707 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
499 | 144 | put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ | |
500 | else | ||
501 | 563 | put_bits(&s->pb, 3, 7); /* backward_f_code */ | |
502 | } | ||
503 | |||
504 | 3576 | put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
505 | |||
506 | 3576 | s->frame_pred_frame_dct = 1; | |
507 |
2/2✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 480 times.
|
3576 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
508 | 3096 | put_header(s, EXT_START_CODE); | |
509 | 3096 | put_bits(&s->pb, 4, 8); /* pic ext */ | |
510 |
2/2✓ Branch 0 taken 924 times.
✓ Branch 1 taken 2172 times.
|
3096 | if (s->pict_type == AV_PICTURE_TYPE_P || |
511 |
2/2✓ Branch 0 taken 563 times.
✓ Branch 1 taken 361 times.
|
924 | s->pict_type == AV_PICTURE_TYPE_B) { |
512 | 2735 | put_bits(&s->pb, 4, s->f_code); | |
513 | 2735 | put_bits(&s->pb, 4, s->f_code); | |
514 | } else { | ||
515 | 361 | put_bits(&s->pb, 8, 255); | |
516 | } | ||
517 |
2/2✓ Branch 0 taken 563 times.
✓ Branch 1 taken 2533 times.
|
3096 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
518 | 563 | put_bits(&s->pb, 4, s->b_code); | |
519 | 563 | put_bits(&s->pb, 4, s->b_code); | |
520 | } else { | ||
521 | 2533 | put_bits(&s->pb, 8, 255); | |
522 | } | ||
523 | 3096 | put_bits(&s->pb, 2, s->intra_dc_precision); | |
524 | |||
525 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3096 times.
|
3096 | av_assert0(s->picture_structure == PICT_FRAME); |
526 | 3096 | put_bits(&s->pb, 2, s->picture_structure); | |
527 |
2/2✓ Branch 0 taken 2146 times.
✓ Branch 1 taken 950 times.
|
3096 | if (s->progressive_sequence) |
528 | 2146 | put_bits(&s->pb, 1, 0); /* no repeat */ | |
529 | else | ||
530 | 950 | put_bits(&s->pb, 1, !!(s->cur_pic.ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)); | |
531 | /* XXX: optimize the generation of this flag with entropy measures */ | ||
532 | 3096 | s->frame_pred_frame_dct = s->progressive_sequence; | |
533 | |||
534 | 3096 | put_bits(&s->pb, 1, s->frame_pred_frame_dct); | |
535 | 3096 | put_bits(&s->pb, 1, s->concealment_motion_vectors); | |
536 | 3096 | put_bits(&s->pb, 1, s->q_scale_type); | |
537 | 3096 | put_bits(&s->pb, 1, s->intra_vlc_format); | |
538 | 3096 | put_bits(&s->pb, 1, s->alternate_scan); | |
539 | 3096 | put_bits(&s->pb, 1, s->repeat_first_field); | |
540 | 3096 | s->progressive_frame = s->progressive_sequence; | |
541 | /* chroma_420_type */ | ||
542 |
2/2✓ Branch 0 taken 2746 times.
✓ Branch 1 taken 350 times.
|
5842 | put_bits(&s->pb, 1, s->chroma_format == |
543 | 2746 | CHROMA_420 ? s->progressive_frame : 0); | |
544 | 3096 | put_bits(&s->pb, 1, s->progressive_frame); | |
545 | 3096 | put_bits(&s->pb, 1, 0); /* composite_display_flag */ | |
546 | } | ||
547 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3576 times.
|
3576 | if (mpeg12->scan_offset) { |
548 | int i; | ||
549 | |||
550 | ✗ | put_header(s, USER_START_CODE); | |
551 | ✗ | for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++) | |
552 | ✗ | put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]); | |
553 | } | ||
554 | 3576 | side_data = av_frame_get_side_data(s->cur_pic.ptr->f, | |
555 | AV_FRAME_DATA_STEREO3D); | ||
556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3576 times.
|
3576 | if (side_data) { |
557 | ✗ | const AVStereo3D *stereo = (AVStereo3D *)side_data->data; | |
558 | uint8_t fpa_type; | ||
559 | |||
560 | ✗ | switch (stereo->type) { | |
561 | ✗ | case AV_STEREO3D_SIDEBYSIDE: | |
562 | ✗ | fpa_type = 0x03; | |
563 | ✗ | break; | |
564 | ✗ | case AV_STEREO3D_TOPBOTTOM: | |
565 | ✗ | fpa_type = 0x04; | |
566 | ✗ | break; | |
567 | ✗ | case AV_STEREO3D_2D: | |
568 | ✗ | fpa_type = 0x08; | |
569 | ✗ | break; | |
570 | ✗ | case AV_STEREO3D_SIDEBYSIDE_QUINCUNX: | |
571 | ✗ | fpa_type = 0x23; | |
572 | ✗ | break; | |
573 | ✗ | default: | |
574 | ✗ | fpa_type = 0; | |
575 | ✗ | break; | |
576 | } | ||
577 | |||
578 | ✗ | if (fpa_type != 0) { | |
579 | ✗ | put_header(s, USER_START_CODE); | |
580 | ✗ | put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier | |
581 | ✗ | put_bits(&s->pb, 8, 'P'); | |
582 | ✗ | put_bits(&s->pb, 8, '3'); | |
583 | ✗ | put_bits(&s->pb, 8, 'D'); | |
584 | ✗ | put_bits(&s->pb, 8, 0x03); // S3D_video_format_length | |
585 | |||
586 | ✗ | put_bits(&s->pb, 1, 1); // reserved_bit | |
587 | ✗ | put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type | |
588 | ✗ | put_bits(&s->pb, 8, 0x04); // reserved_data[0] | |
589 | ✗ | put_bits(&s->pb, 8, 0xFF); // reserved_data[1] | |
590 | } | ||
591 | } | ||
592 | |||
593 |
2/2✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 480 times.
|
3576 | if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) { |
594 | 3096 | side_data = av_frame_get_side_data(s->cur_pic.ptr->f, | |
595 | AV_FRAME_DATA_A53_CC); | ||
596 |
2/2✓ Branch 0 taken 135 times.
✓ Branch 1 taken 2961 times.
|
3096 | if (side_data) { |
597 |
3/4✓ Branch 0 taken 134 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
|
269 | if (side_data->size <= A53_MAX_CC_COUNT * 3 && side_data->size % 3 == 0) { |
598 | 134 | int i = 0; | |
599 | |||
600 | 134 | put_header (s, USER_START_CODE); | |
601 | |||
602 | 134 | put_bits(&s->pb, 8, 'G'); // user_identifier | |
603 | 134 | put_bits(&s->pb, 8, 'A'); | |
604 | 134 | put_bits(&s->pb, 8, '9'); | |
605 | 134 | put_bits(&s->pb, 8, '4'); | |
606 | 134 | put_bits(&s->pb, 8, 3); // user_data_type_code | |
607 | 134 | put_bits(&s->pb, 8, | |
608 | 134 | (side_data->size / 3 & A53_MAX_CC_COUNT) | 0x40); // flags, cc_count | |
609 | 134 | put_bits(&s->pb, 8, 0xff); // em_data | |
610 | |||
611 |
2/2✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 134 times.
|
8174 | for (i = 0; i < side_data->size; i++) |
612 | 8040 | put_bits(&s->pb, 8, side_data->data[i]); | |
613 | |||
614 | 134 | put_bits(&s->pb, 8, 0xff); // marker_bits | |
615 | } else { | ||
616 | 1 | av_log(s->avctx, AV_LOG_WARNING, | |
617 | "Closed Caption size (%"SIZE_SPECIFIER") can not exceed " | ||
618 | 1 | "93 bytes and must be a multiple of 3\n", side_data->size); | |
619 | } | ||
620 | } | ||
621 | } | ||
622 | |||
623 | 3576 | s->mb_y = 0; | |
624 | 3576 | ff_mpeg1_encode_slice_header(s); | |
625 | 3576 | } | |
626 | |||
627 | 1455658 | static inline void put_mb_modes(MpegEncContext *s, int n, int bits, | |
628 | int has_mv, int field_motion) | ||
629 | { | ||
630 | 1455658 | put_bits(&s->pb, n, bits); | |
631 |
2/2✓ Branch 0 taken 698812 times.
✓ Branch 1 taken 756846 times.
|
1455658 | if (!s->frame_pred_frame_dct) { |
632 |
2/2✓ Branch 0 taken 364218 times.
✓ Branch 1 taken 334594 times.
|
698812 | if (has_mv) |
633 | /* motion_type: frame/field */ | ||
634 | 364218 | put_bits(&s->pb, 2, 2 - field_motion); | |
635 | 698812 | put_bits(&s->pb, 1, s->interlaced_dct); | |
636 | } | ||
637 | 1455658 | } | |
638 | |||
639 | // RAL: Parameter added: f_or_b_code | ||
640 | 3283710 | static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) | |
641 | { | ||
642 |
2/2✓ Branch 0 taken 1313138 times.
✓ Branch 1 taken 1970572 times.
|
3283710 | if (val == 0) { |
643 | /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */ | ||
644 | 1313138 | put_bits(&s->pb, 1, 0x01); | |
645 | } else { | ||
646 | int code, sign, bits; | ||
647 | 1970572 | int bit_size = f_or_b_code - 1; | |
648 | 1970572 | int range = 1 << bit_size; | |
649 | /* modulo encoding */ | ||
650 | 1970572 | val = sign_extend(val, 5 + bit_size); | |
651 | |||
652 |
2/2✓ Branch 0 taken 950640 times.
✓ Branch 1 taken 1019932 times.
|
1970572 | if (val >= 0) { |
653 | 950640 | val--; | |
654 | 950640 | code = (val >> bit_size) + 1; | |
655 | 950640 | bits = val & (range - 1); | |
656 | 950640 | sign = 0; | |
657 | } else { | ||
658 | 1019932 | val = -val; | |
659 | 1019932 | val--; | |
660 | 1019932 | code = (val >> bit_size) + 1; | |
661 | 1019932 | bits = val & (range - 1); | |
662 | 1019932 | sign = 1; | |
663 | } | ||
664 | |||
665 | av_assert2(code > 0 && code <= 16); | ||
666 | |||
667 | 1970572 | put_bits(&s->pb, | |
668 | 1970572 | ff_mpeg12_mbMotionVectorTable[code][1], | |
669 | 1970572 | ff_mpeg12_mbMotionVectorTable[code][0]); | |
670 | |||
671 | 1970572 | put_bits(&s->pb, 1, sign); | |
672 |
2/2✓ Branch 0 taken 1418848 times.
✓ Branch 1 taken 551724 times.
|
1970572 | if (bit_size > 0) |
673 | 1418848 | put_bits(&s->pb, bit_size, bits); | |
674 | } | ||
675 | 3283710 | } | |
676 | |||
677 | 3438044 | static inline void encode_dc(MpegEncContext *s, int diff, int component) | |
678 | { | ||
679 | 3438044 | unsigned int diff_u = diff + 255; | |
680 |
2/2✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 3275834 times.
|
3438044 | if (diff_u >= 511) { |
681 | int index; | ||
682 | |||
683 |
2/2✓ Branch 0 taken 113286 times.
✓ Branch 1 taken 48924 times.
|
162210 | if (diff < 0) { |
684 | 113286 | index = av_log2_16bit(-2 * diff); | |
685 | 113286 | diff--; | |
686 | } else { | ||
687 | 48924 | index = av_log2_16bit(2 * diff); | |
688 | } | ||
689 |
2/2✓ Branch 0 taken 91380 times.
✓ Branch 1 taken 70830 times.
|
162210 | if (component == 0) |
690 | 91380 | put_bits(&s->pb, | |
691 | 91380 | ff_mpeg12_vlc_dc_lum_bits[index] + index, | |
692 | 91380 | (ff_mpeg12_vlc_dc_lum_code[index] << index) + | |
693 | 91380 | av_zero_extend(diff, index)); | |
694 | else | ||
695 | 70830 | put_bits(&s->pb, | |
696 | 70830 | ff_mpeg12_vlc_dc_chroma_bits[index] + index, | |
697 | 70830 | (ff_mpeg12_vlc_dc_chroma_code[index] << index) + | |
698 | 70830 | av_zero_extend(diff, index)); | |
699 | } else { | ||
700 |
2/2✓ Branch 0 taken 1806968 times.
✓ Branch 1 taken 1468866 times.
|
3275834 | if (component == 0) |
701 | 1806968 | put_bits(&s->pb, | |
702 | 1806968 | mpeg1_lum_dc_uni[diff + 255] & 0xFF, | |
703 | 1806968 | mpeg1_lum_dc_uni[diff + 255] >> 8); | |
704 | else | ||
705 | 1468866 | put_bits(&s->pb, | |
706 | 1468866 | mpeg1_chr_dc_uni[diff + 255] & 0xFF, | |
707 | 1468866 | mpeg1_chr_dc_uni[diff + 255] >> 8); | |
708 | } | ||
709 | 3438044 | } | |
710 | |||
711 | 7010842 | static void mpeg1_encode_block(MpegEncContext *s, const int16_t *block, int n) | |
712 | { | ||
713 | int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; | ||
714 | int code, component; | ||
715 | 7010842 | const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table; | |
716 | |||
717 | 7010842 | last_index = s->block_last_index[n]; | |
718 | |||
719 | /* DC coef */ | ||
720 |
2/2✓ Branch 0 taken 3438044 times.
✓ Branch 1 taken 3572798 times.
|
7010842 | if (s->mb_intra) { |
721 |
2/2✓ Branch 0 taken 1539696 times.
✓ Branch 1 taken 1898348 times.
|
3438044 | component = (n <= 3 ? 0 : (n & 1) + 1); |
722 | 3438044 | dc = block[0]; /* overflow is impossible */ | |
723 | 3438044 | diff = dc - s->last_dc[component]; | |
724 | 3438044 | encode_dc(s, diff, component); | |
725 | 3438044 | s->last_dc[component] = dc; | |
726 | 3438044 | i = 1; | |
727 |
2/2✓ Branch 0 taken 2641700 times.
✓ Branch 1 taken 796344 times.
|
3438044 | if (s->intra_vlc_format) |
728 | 2641700 | table_vlc = ff_mpeg2_vlc_table; | |
729 | } else { | ||
730 | /* encode the first coefficient: needs to be done here because | ||
731 | * it is handled slightly differently */ | ||
732 | 3572798 | level = block[0]; | |
733 |
2/2✓ Branch 0 taken 1068212 times.
✓ Branch 1 taken 2504586 times.
|
3572798 | if (abs(level) == 1) { |
734 | 1068212 | code = ((uint32_t)level >> 31); /* the sign bit */ | |
735 | 1068212 | put_bits(&s->pb, 2, code | 0x02); | |
736 | 1068212 | i = 1; | |
737 | } else { | ||
738 | 2504586 | i = 0; | |
739 | 2504586 | last_non_zero = -1; | |
740 | 2504586 | goto next_coef; | |
741 | } | ||
742 | } | ||
743 | |||
744 | /* now quantify & encode AC coefs */ | ||
745 | 4506256 | last_non_zero = i - 1; | |
746 | |||
747 |
2/2✓ Branch 0 taken 163568865 times.
✓ Branch 1 taken 7010842 times.
|
170579707 | for (; i <= last_index; i++) { |
748 | 163568865 | j = s->intra_scantable.permutated[i]; | |
749 | 163568865 | level = block[j]; | |
750 | |||
751 | 166073451 | next_coef: | |
752 | /* encode using VLC */ | ||
753 |
2/2✓ Branch 0 taken 68880903 times.
✓ Branch 1 taken 97192548 times.
|
166073451 | if (level != 0) { |
754 | 68880903 | run = i - last_non_zero - 1; | |
755 | |||
756 | 68880903 | alevel = level; | |
757 | 68880903 | MASK_ABS(sign, alevel); | |
758 | 68880903 | sign &= 1; | |
759 | |||
760 |
2/2✓ Branch 0 taken 66712686 times.
✓ Branch 1 taken 2168217 times.
|
68880903 | if (alevel <= mpeg12_max_level[run]) { |
761 | 66712686 | code = mpeg12_index_run[run] + alevel - 1; | |
762 | /* store the VLC & sign at once */ | ||
763 | 66712686 | put_bits(&s->pb, table_vlc[code][1] + 1, | |
764 | 66712686 | (table_vlc[code][0] << 1) + sign); | |
765 | } else { | ||
766 | /* Escape seems to be pretty rare <5% so I do not optimize it; | ||
767 | * the following value is the common escape value for both | ||
768 | * possible tables (i.e. table_vlc[111]). */ | ||
769 | 2168217 | put_bits(&s->pb, 6, 0x01); | |
770 | /* escape: only clip in this case */ | ||
771 | 2168217 | put_bits(&s->pb, 6, run); | |
772 |
2/2✓ Branch 0 taken 82680 times.
✓ Branch 1 taken 2085537 times.
|
2168217 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) { |
773 |
1/2✓ Branch 0 taken 82680 times.
✗ Branch 1 not taken.
|
82680 | if (alevel < 128) { |
774 | 82680 | put_sbits(&s->pb, 8, level); | |
775 | } else { | ||
776 | ✗ | if (level < 0) | |
777 | ✗ | put_bits(&s->pb, 16, 0x8001 + level + 255); | |
778 | else | ||
779 | ✗ | put_sbits(&s->pb, 16, level); | |
780 | } | ||
781 | } else { | ||
782 | 2085537 | put_sbits(&s->pb, 12, level); | |
783 | } | ||
784 | } | ||
785 | 68880903 | last_non_zero = i; | |
786 | } | ||
787 | } | ||
788 | /* end of block */ | ||
789 | 7010842 | put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]); | |
790 | 7010842 | } | |
791 | |||
792 | 1910328 | static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, | |
793 | const int16_t block[8][64], | ||
794 | int motion_x, int motion_y, | ||
795 | int mb_block_count, | ||
796 | int chroma_y_shift) | ||
797 | { | ||
798 | /* MPEG-1 is always 420. */ | ||
799 | #define IS_MPEG1(s) (chroma_y_shift == 1 && (s)->codec_id == AV_CODEC_ID_MPEG1VIDEO) | ||
800 | int i, cbp; | ||
801 | 1910328 | const int mb_x = s->mb_x; | |
802 | 1910328 | const int mb_y = s->mb_y; | |
803 |
4/4✓ Branch 0 taken 331049 times.
✓ Branch 1 taken 1579279 times.
✓ Branch 2 taken 326392 times.
✓ Branch 3 taken 4657 times.
|
1910328 | const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y; |
804 | |||
805 | /* compute cbp */ | ||
806 | 1910328 | cbp = 0; | |
807 |
2/2✓ Branch 0 taken 12747878 times.
✓ Branch 1 taken 1910328 times.
|
14658206 | for (i = 0; i < mb_block_count; i++) |
808 |
2/2✓ Branch 0 taken 7010842 times.
✓ Branch 1 taken 5737036 times.
|
12747878 | if (s->block_last_index[i] >= 0) |
809 | 7010842 | cbp |= 1 << (mb_block_count - 1 - i); | |
810 | |||
811 |
6/6✓ Branch 0 taken 454670 times.
✓ Branch 1 taken 1455658 times.
✓ Branch 2 taken 443337 times.
✓ Branch 3 taken 11333 times.
✓ Branch 4 taken 423114 times.
✓ Branch 5 taken 20223 times.
|
1910328 | if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 && |
812 |
2/2✓ Branch 0 taken 12180 times.
✓ Branch 1 taken 410934 times.
|
423114 | (mb_x != s->mb_width - 1 || |
813 |
6/6✓ Branch 0 taken 11420 times.
✓ Branch 1 taken 760 times.
✓ Branch 2 taken 9460 times.
✓ Branch 3 taken 1960 times.
✓ Branch 4 taken 552 times.
✓ Branch 5 taken 8908 times.
|
12180 | (mb_y != s->end_mb_y - 1 && IS_MPEG1(s))) && |
814 |
4/4✓ Branch 0 taken 241250 times.
✓ Branch 1 taken 170236 times.
✓ Branch 2 taken 112067 times.
✓ Branch 3 taken 129183 times.
|
411486 | ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) || |
815 |
4/4✓ Branch 0 taken 170236 times.
✓ Branch 1 taken 112067 times.
✓ Branch 2 taken 22008 times.
✓ Branch 3 taken 148228 times.
|
282303 | (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir && |
816 | 22008 | (((s->mv_dir & MV_DIR_FORWARD) | |
817 | 20311 | ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) | | |
818 |
4/4✓ Branch 0 taken 20311 times.
✓ Branch 1 taken 1697 times.
✓ Branch 2 taken 4342 times.
✓ Branch 3 taken 17666 times.
|
44016 | (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | |
819 | 22008 | ((s->mv_dir & MV_DIR_BACKWARD) | |
820 | 21679 | ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) | | |
821 |
2/2✓ Branch 0 taken 21679 times.
✓ Branch 1 taken 329 times.
|
22008 | (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { |
822 | 133525 | s->mb_skip_run++; | |
823 | 133525 | s->qscale -= s->dquant; | |
824 | 133525 | s->misc_bits++; | |
825 | 133525 | s->last_bits++; | |
826 |
2/2✓ Branch 0 taken 129183 times.
✓ Branch 1 taken 4342 times.
|
133525 | if (s->pict_type == AV_PICTURE_TYPE_P) { |
827 | 129183 | s->last_mv[0][0][0] = | |
828 | 129183 | s->last_mv[0][0][1] = | |
829 | 129183 | s->last_mv[0][1][0] = | |
830 | 129183 | s->last_mv[0][1][1] = 0; | |
831 | } | ||
832 | } else { | ||
833 |
2/2✓ Branch 0 taken 326392 times.
✓ Branch 1 taken 1450411 times.
|
1776803 | if (first_mb) { |
834 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 326392 times.
|
326392 | av_assert0(s->mb_skip_run == 0); |
835 | 326392 | encode_mb_skip_run(s, s->mb_x); | |
836 | } else { | ||
837 | 1450411 | encode_mb_skip_run(s, s->mb_skip_run); | |
838 | } | ||
839 | |||
840 |
2/2✓ Branch 0 taken 400936 times.
✓ Branch 1 taken 1375867 times.
|
1776803 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
841 |
3/4✓ Branch 0 taken 43090 times.
✓ Branch 1 taken 357846 times.
✓ Branch 2 taken 43090 times.
✗ Branch 3 not taken.
|
400936 | if (s->dquant && cbp) { |
842 | /* macroblock_type: macroblock_quant = 1 */ | ||
843 | 43090 | put_mb_modes(s, 2, 1, 0, 0); | |
844 | 43090 | put_qscale(s); | |
845 | } else { | ||
846 | /* macroblock_type: macroblock_quant = 0 */ | ||
847 | 357846 | put_mb_modes(s, 1, 1, 0, 0); | |
848 | 357846 | s->qscale -= s->dquant; | |
849 | } | ||
850 | 400936 | s->misc_bits += get_bits_diff(s); | |
851 | 400936 | s->i_count++; | |
852 |
2/2✓ Branch 0 taken 73651 times.
✓ Branch 1 taken 1302216 times.
|
1375867 | } else if (s->mb_intra) { |
853 |
3/4✓ Branch 0 taken 11835 times.
✓ Branch 1 taken 61816 times.
✓ Branch 2 taken 11835 times.
✗ Branch 3 not taken.
|
73651 | if (s->dquant && cbp) { |
854 | 11835 | put_mb_modes(s, 6, 0x01, 0, 0); | |
855 | 11835 | put_qscale(s); | |
856 | } else { | ||
857 | 61816 | put_mb_modes(s, 5, 0x03, 0, 0); | |
858 | 61816 | s->qscale -= s->dquant; | |
859 | } | ||
860 | 73651 | s->misc_bits += get_bits_diff(s); | |
861 | 73651 | s->i_count++; | |
862 | 73651 | memset(s->last_mv, 0, sizeof(s->last_mv)); | |
863 |
2/2✓ Branch 0 taken 721273 times.
✓ Branch 1 taken 580943 times.
|
1302216 | } else if (s->pict_type == AV_PICTURE_TYPE_P) { |
864 |
2/2✓ Branch 0 taken 698424 times.
✓ Branch 1 taken 22849 times.
|
721273 | if (s->mv_type == MV_TYPE_16X16) { |
865 |
2/2✓ Branch 0 taken 573435 times.
✓ Branch 1 taken 124989 times.
|
698424 | if (cbp != 0) { |
866 |
2/2✓ Branch 0 taken 61231 times.
✓ Branch 1 taken 512204 times.
|
573435 | if ((motion_x | motion_y) == 0) { |
867 |
2/2✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 59677 times.
|
61231 | if (s->dquant) { |
868 | /* macroblock_pattern & quant */ | ||
869 | 1554 | put_mb_modes(s, 5, 1, 0, 0); | |
870 | 1554 | put_qscale(s); | |
871 | } else { | ||
872 | /* macroblock_pattern only */ | ||
873 | 59677 | put_mb_modes(s, 2, 1, 0, 0); | |
874 | } | ||
875 | 61231 | s->misc_bits += get_bits_diff(s); | |
876 | } else { | ||
877 |
2/2✓ Branch 0 taken 72389 times.
✓ Branch 1 taken 439815 times.
|
512204 | if (s->dquant) { |
878 | 72389 | put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */ | |
879 | 72389 | put_qscale(s); | |
880 | } else { | ||
881 | 439815 | put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */ | |
882 | } | ||
883 | 512204 | s->misc_bits += get_bits_diff(s); | |
884 | // RAL: f_code parameter added | ||
885 | 512204 | mpeg1_encode_motion(s, | |
886 | 512204 | motion_x - s->last_mv[0][0][0], | |
887 | s->f_code); | ||
888 | // RAL: f_code parameter added | ||
889 | 512204 | mpeg1_encode_motion(s, | |
890 | 512204 | motion_y - s->last_mv[0][0][1], | |
891 | s->f_code); | ||
892 | 512204 | s->mv_bits += get_bits_diff(s); | |
893 | } | ||
894 | } else { | ||
895 | 124989 | put_bits(&s->pb, 3, 1); /* motion only */ | |
896 |
2/2✓ Branch 0 taken 17533 times.
✓ Branch 1 taken 107456 times.
|
124989 | if (!s->frame_pred_frame_dct) |
897 | 17533 | put_bits(&s->pb, 2, 2); /* motion_type: frame */ | |
898 | 124989 | s->misc_bits += get_bits_diff(s); | |
899 | // RAL: f_code parameter added | ||
900 | 124989 | mpeg1_encode_motion(s, | |
901 | 124989 | motion_x - s->last_mv[0][0][0], | |
902 | s->f_code); | ||
903 | // RAL: f_code parameter added | ||
904 | 124989 | mpeg1_encode_motion(s, | |
905 | 124989 | motion_y - s->last_mv[0][0][1], | |
906 | s->f_code); | ||
907 | 124989 | s->qscale -= s->dquant; | |
908 | 124989 | s->mv_bits += get_bits_diff(s); | |
909 | } | ||
910 | 698424 | s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x; | |
911 | 698424 | s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y; | |
912 | } else { | ||
913 | av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); | ||
914 | |||
915 |
2/2✓ Branch 0 taken 21344 times.
✓ Branch 1 taken 1505 times.
|
22849 | if (cbp) { |
916 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21344 times.
|
21344 | if (s->dquant) { |
917 | ✗ | put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */ | |
918 | ✗ | put_qscale(s); | |
919 | } else { | ||
920 | 21344 | put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */ | |
921 | } | ||
922 | } else { | ||
923 | 1505 | put_bits(&s->pb, 3, 1); /* motion only */ | |
924 | 1505 | put_bits(&s->pb, 2, 1); /* motion_type: field */ | |
925 | 1505 | s->qscale -= s->dquant; | |
926 | } | ||
927 | 22849 | s->misc_bits += get_bits_diff(s); | |
928 |
2/2✓ Branch 0 taken 45698 times.
✓ Branch 1 taken 22849 times.
|
68547 | for (i = 0; i < 2; i++) { |
929 | 45698 | put_bits(&s->pb, 1, s->field_select[0][i]); | |
930 | 45698 | mpeg1_encode_motion(s, | |
931 | 45698 | s->mv[0][i][0] - s->last_mv[0][i][0], | |
932 | s->f_code); | ||
933 | 45698 | mpeg1_encode_motion(s, | |
934 | 45698 | s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1), | |
935 | s->f_code); | ||
936 | 45698 | s->last_mv[0][i][0] = s->mv[0][i][0]; | |
937 | 45698 | s->last_mv[0][i][1] = 2 * s->mv[0][i][1]; | |
938 | } | ||
939 | 22849 | s->mv_bits += get_bits_diff(s); | |
940 | } | ||
941 |
2/2✓ Branch 0 taken 594779 times.
✓ Branch 1 taken 126494 times.
|
721273 | if (cbp) { |
942 |
2/2✓ Branch 0 taken 519967 times.
✓ Branch 1 taken 74812 times.
|
594779 | if (chroma_y_shift) { |
943 | 519967 | put_bits(&s->pb, | |
944 | 519967 | ff_mpeg12_mbPatTable[cbp][1], | |
945 | 519967 | ff_mpeg12_mbPatTable[cbp][0]); | |
946 | } else { | ||
947 | 74812 | put_bits(&s->pb, | |
948 | 74812 | ff_mpeg12_mbPatTable[cbp >> 2][1], | |
949 | 74812 | ff_mpeg12_mbPatTable[cbp >> 2][0]); | |
950 | 74812 | put_sbits(&s->pb, 2, cbp); | |
951 | } | ||
952 | } | ||
953 | } else { | ||
954 |
2/2✓ Branch 0 taken 488110 times.
✓ Branch 1 taken 92833 times.
|
580943 | if (s->mv_type == MV_TYPE_16X16) { |
955 |
2/2✓ Branch 0 taken 312597 times.
✓ Branch 1 taken 175513 times.
|
488110 | if (cbp) { // With coded bloc pattern |
956 |
2/2✓ Branch 0 taken 53290 times.
✓ Branch 1 taken 259307 times.
|
312597 | if (s->dquant) { |
957 |
2/2✓ Branch 0 taken 11218 times.
✓ Branch 1 taken 42072 times.
|
53290 | if (s->mv_dir == MV_DIR_FORWARD) |
958 | 11218 | put_mb_modes(s, 6, 3, 1, 0); | |
959 | else | ||
960 | 42072 | put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0); | |
961 | 53290 | put_qscale(s); | |
962 | } else { | ||
963 | 259307 | put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0); | |
964 | } | ||
965 | } else { // No coded bloc pattern | ||
966 | 175513 | put_bits(&s->pb, 5 - s->mv_dir, 2); | |
967 |
2/2✓ Branch 0 taken 82021 times.
✓ Branch 1 taken 93492 times.
|
175513 | if (!s->frame_pred_frame_dct) |
968 | 82021 | put_bits(&s->pb, 2, 2); /* motion_type: frame */ | |
969 | 175513 | s->qscale -= s->dquant; | |
970 | } | ||
971 | 488110 | s->misc_bits += get_bits_diff(s); | |
972 |
2/2✓ Branch 0 taken 351301 times.
✓ Branch 1 taken 136809 times.
|
488110 | if (s->mv_dir & MV_DIR_FORWARD) { |
973 | 351301 | mpeg1_encode_motion(s, | |
974 | 351301 | s->mv[0][0][0] - s->last_mv[0][0][0], | |
975 | s->f_code); | ||
976 | 351301 | mpeg1_encode_motion(s, | |
977 | 351301 | s->mv[0][0][1] - s->last_mv[0][0][1], | |
978 | s->f_code); | ||
979 | 351301 | s->last_mv[0][0][0] = | |
980 | 351301 | s->last_mv[0][1][0] = s->mv[0][0][0]; | |
981 | 351301 | s->last_mv[0][0][1] = | |
982 | 351301 | s->last_mv[0][1][1] = s->mv[0][0][1]; | |
983 | } | ||
984 |
2/2✓ Branch 0 taken 369943 times.
✓ Branch 1 taken 118167 times.
|
488110 | if (s->mv_dir & MV_DIR_BACKWARD) { |
985 | 369943 | mpeg1_encode_motion(s, | |
986 | 369943 | s->mv[1][0][0] - s->last_mv[1][0][0], | |
987 | s->b_code); | ||
988 | 369943 | mpeg1_encode_motion(s, | |
989 | 369943 | s->mv[1][0][1] - s->last_mv[1][0][1], | |
990 | s->b_code); | ||
991 | 369943 | s->last_mv[1][0][0] = | |
992 | 369943 | s->last_mv[1][1][0] = s->mv[1][0][0]; | |
993 | 369943 | s->last_mv[1][0][1] = | |
994 | 369943 | s->last_mv[1][1][1] = s->mv[1][0][1]; | |
995 | } | ||
996 | } else { | ||
997 | av_assert2(s->mv_type == MV_TYPE_FIELD); | ||
998 | av_assert2(!s->frame_pred_frame_dct); | ||
999 |
2/2✓ Branch 0 taken 73695 times.
✓ Branch 1 taken 19138 times.
|
92833 | if (cbp) { // With coded bloc pattern |
1000 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 73695 times.
|
73695 | if (s->dquant) { |
1001 | ✗ | if (s->mv_dir == MV_DIR_FORWARD) | |
1002 | ✗ | put_mb_modes(s, 6, 3, 1, 1); | |
1003 | else | ||
1004 | ✗ | put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1); | |
1005 | ✗ | put_qscale(s); | |
1006 | } else { | ||
1007 | 73695 | put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1); | |
1008 | } | ||
1009 | } else { // No coded bloc pattern | ||
1010 | 19138 | put_bits(&s->pb, 5 - s->mv_dir, 2); | |
1011 | 19138 | put_bits(&s->pb, 2, 1); /* motion_type: field */ | |
1012 | 19138 | s->qscale -= s->dquant; | |
1013 | } | ||
1014 | 92833 | s->misc_bits += get_bits_diff(s); | |
1015 |
2/2✓ Branch 0 taken 59191 times.
✓ Branch 1 taken 33642 times.
|
92833 | if (s->mv_dir & MV_DIR_FORWARD) { |
1016 |
2/2✓ Branch 0 taken 118382 times.
✓ Branch 1 taken 59191 times.
|
177573 | for (i = 0; i < 2; i++) { |
1017 | 118382 | put_bits(&s->pb, 1, s->field_select[0][i]); | |
1018 | 118382 | mpeg1_encode_motion(s, | |
1019 | 118382 | s->mv[0][i][0] - s->last_mv[0][i][0], | |
1020 | s->f_code); | ||
1021 | 118382 | mpeg1_encode_motion(s, | |
1022 | 118382 | s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1), | |
1023 | s->f_code); | ||
1024 | 118382 | s->last_mv[0][i][0] = s->mv[0][i][0]; | |
1025 | 118382 | s->last_mv[0][i][1] = s->mv[0][i][1] * 2; | |
1026 | } | ||
1027 | } | ||
1028 |
2/2✓ Branch 0 taken 59669 times.
✓ Branch 1 taken 33164 times.
|
92833 | if (s->mv_dir & MV_DIR_BACKWARD) { |
1029 |
2/2✓ Branch 0 taken 119338 times.
✓ Branch 1 taken 59669 times.
|
179007 | for (i = 0; i < 2; i++) { |
1030 | 119338 | put_bits(&s->pb, 1, s->field_select[1][i]); | |
1031 | 119338 | mpeg1_encode_motion(s, | |
1032 | 119338 | s->mv[1][i][0] - s->last_mv[1][i][0], | |
1033 | s->b_code); | ||
1034 | 119338 | mpeg1_encode_motion(s, | |
1035 | 119338 | s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1), | |
1036 | s->b_code); | ||
1037 | 119338 | s->last_mv[1][i][0] = s->mv[1][i][0]; | |
1038 | 119338 | s->last_mv[1][i][1] = s->mv[1][i][1] * 2; | |
1039 | } | ||
1040 | } | ||
1041 | } | ||
1042 | 580943 | s->mv_bits += get_bits_diff(s); | |
1043 |
2/2✓ Branch 0 taken 386292 times.
✓ Branch 1 taken 194651 times.
|
580943 | if (cbp) { |
1044 |
2/2✓ Branch 0 taken 206130 times.
✓ Branch 1 taken 180162 times.
|
386292 | if (chroma_y_shift) { |
1045 | 206130 | put_bits(&s->pb, | |
1046 | 206130 | ff_mpeg12_mbPatTable[cbp][1], | |
1047 | 206130 | ff_mpeg12_mbPatTable[cbp][0]); | |
1048 | } else { | ||
1049 | 180162 | put_bits(&s->pb, | |
1050 | 180162 | ff_mpeg12_mbPatTable[cbp >> 2][1], | |
1051 | 180162 | ff_mpeg12_mbPatTable[cbp >> 2][0]); | |
1052 | 180162 | put_sbits(&s->pb, 2, cbp); | |
1053 | } | ||
1054 | } | ||
1055 | } | ||
1056 |
2/2✓ Branch 0 taken 11945432 times.
✓ Branch 1 taken 1776803 times.
|
13722235 | for (i = 0; i < mb_block_count; i++) |
1057 |
2/2✓ Branch 0 taken 7010842 times.
✓ Branch 1 taken 4934590 times.
|
11945432 | if (cbp & (1 << (mb_block_count - 1 - i))) |
1058 | 7010842 | mpeg1_encode_block(s, block[i], i); | |
1059 | 1776803 | s->mb_skip_run = 0; | |
1060 |
2/2✓ Branch 0 taken 474587 times.
✓ Branch 1 taken 1302216 times.
|
1776803 | if (s->mb_intra) |
1061 | 474587 | s->i_tex_bits += get_bits_diff(s); | |
1062 | else | ||
1063 | 1302216 | s->p_tex_bits += get_bits_diff(s); | |
1064 | } | ||
1065 | 1910328 | } | |
1066 | |||
1067 | 1910328 | void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64], | |
1068 | int motion_x, int motion_y) | ||
1069 | { | ||
1070 |
2/2✓ Branch 0 taken 1267373 times.
✓ Branch 1 taken 642955 times.
|
1910328 | if (s->chroma_format == CHROMA_420) |
1071 | 1267373 | mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6, 1); | |
1072 | else | ||
1073 | 642955 | mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8, 0); | |
1074 | 1910328 | } | |
1075 | |||
1076 | 55 | static av_cold void mpeg12_encode_init_static(void) | |
1077 | { | ||
1078 | 55 | ff_rl_init_level_run(mpeg12_max_level, mpeg12_index_run, | |
1079 | ff_mpeg12_run, ff_mpeg12_level, MPEG12_RL_NB_ELEMS); | ||
1080 | |||
1081 | 55 | ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run, | |
1082 | ff_mpeg1_vlc_table, uni_mpeg1_ac_vlc_len); | ||
1083 | 55 | ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run, | |
1084 | ff_mpeg2_vlc_table, uni_mpeg2_ac_vlc_len); | ||
1085 | |||
1086 | /* build unified dc encoding tables */ | ||
1087 |
2/2✓ Branch 0 taken 28105 times.
✓ Branch 1 taken 55 times.
|
28160 | for (int i = -255; i < 256; i++) { |
1088 | int adiff, index; | ||
1089 | int bits, code; | ||
1090 | 28105 | int diff = i; | |
1091 | |||
1092 | 28105 | adiff = FFABS(diff); | |
1093 |
2/2✓ Branch 0 taken 14025 times.
✓ Branch 1 taken 14080 times.
|
28105 | if (diff < 0) |
1094 | 14025 | diff--; | |
1095 | 28105 | index = av_log2(2 * adiff); | |
1096 | |||
1097 | 28105 | bits = ff_mpeg12_vlc_dc_lum_bits[index] + index; | |
1098 | 28105 | code = (ff_mpeg12_vlc_dc_lum_code[index] << index) + | |
1099 | 28105 | av_zero_extend(diff, index); | |
1100 | 28105 | mpeg1_lum_dc_uni[i + 255] = bits + (code << 8); | |
1101 | |||
1102 | 28105 | bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index; | |
1103 | 28105 | code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) + | |
1104 | 28105 | av_zero_extend(diff, index); | |
1105 | 28105 | mpeg1_chr_dc_uni[i + 255] = bits + (code << 8); | |
1106 | } | ||
1107 | |||
1108 |
2/2✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
|
440 | for (int f_code = 1; f_code <= MAX_FCODE; f_code++) |
1109 |
2/2✓ Branch 0 taken 6308225 times.
✓ Branch 1 taken 385 times.
|
6308610 | for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) { |
1110 | int len; | ||
1111 | |||
1112 |
2/2✓ Branch 0 taken 385 times.
✓ Branch 1 taken 6307840 times.
|
6308225 | if (mv == 0) { |
1113 | 385 | len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */ | |
1114 | } else { | ||
1115 | int val, bit_size, code; | ||
1116 | |||
1117 | 6307840 | bit_size = f_code - 1; | |
1118 | |||
1119 | 6307840 | val = mv; | |
1120 |
2/2✓ Branch 0 taken 3153920 times.
✓ Branch 1 taken 3153920 times.
|
6307840 | if (val < 0) |
1121 | 3153920 | val = -val; | |
1122 | 6307840 | val--; | |
1123 | 6307840 | code = (val >> bit_size) + 1; | |
1124 |
2/2✓ Branch 0 taken 223520 times.
✓ Branch 1 taken 6084320 times.
|
6307840 | if (code < 17) |
1125 | 223520 | len = ff_mpeg12_mbMotionVectorTable[code][1] + | |
1126 | 1 + bit_size; | ||
1127 | else | ||
1128 | 6084320 | len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ + | |
1129 | 2 + bit_size; | ||
1130 | } | ||
1131 | |||
1132 | 6308225 | mv_penalty[f_code][mv + MAX_DMV] = len; | |
1133 | } | ||
1134 | |||
1135 | |||
1136 |
2/2✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
|
440 | for (int f_code = MAX_FCODE; f_code > 0; f_code--) |
1137 |
2/2✓ Branch 0 taken 223520 times.
✓ Branch 1 taken 385 times.
|
223905 | for (int mv = -(8 << f_code); mv < (8 << f_code); mv++) |
1138 | 223520 | fcode_tab[mv + MAX_MV] = f_code; | |
1139 | 55 | } | |
1140 | |||
1141 | 55 | av_cold void ff_mpeg1_encode_init(MpegEncContext *s) | |
1142 | { | ||
1143 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
1144 | |||
1145 | 55 | s->y_dc_scale_table = | |
1146 | 55 | s->c_dc_scale_table = ff_mpeg12_dc_scale_table[s->intra_dc_precision]; | |
1147 | |||
1148 | 55 | s->me.mv_penalty = mv_penalty; | |
1149 | 55 | s->fcode_tab = fcode_tab; | |
1150 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 44 times.
|
55 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) { |
1151 | 11 | s->min_qcoeff = -255; | |
1152 | 11 | s->max_qcoeff = 255; | |
1153 | } else { | ||
1154 | 44 | s->min_qcoeff = -2047; | |
1155 | 44 | s->max_qcoeff = 2047; | |
1156 | 44 | s->mpeg_quant = 1; | |
1157 | } | ||
1158 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 42 times.
|
55 | if (s->intra_vlc_format) { |
1159 | 13 | s->intra_ac_vlc_length = | |
1160 | 13 | s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len; | |
1161 | } else { | ||
1162 | 42 | s->intra_ac_vlc_length = | |
1163 | 42 | s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len; | |
1164 | } | ||
1165 | 55 | s->inter_ac_vlc_length = | |
1166 | 55 | s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len; | |
1167 | |||
1168 | 55 | ff_thread_once(&init_static_once, mpeg12_encode_init_static); | |
1169 | 55 | } | |
1170 | |||
1171 | #define OFFSET(x) offsetof(MPEG12EncContext, x) | ||
1172 | #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | ||
1173 | #define COMMON_OPTS \ | ||
1174 | { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \ | ||
1175 | OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE },\ | ||
1176 | { "drop_frame_timecode", "Timecode is in drop frame format.", \ | ||
1177 | OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \ | ||
1178 | { "scan_offset", "Reserve space for SVCD scan offset user data.", \ | ||
1179 | OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \ | ||
1180 | { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \ | ||
1181 | OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, VE}, \ | ||
1182 | FF_MPV_COMMON_BFRAME_OPTS | ||
1183 | |||
1184 | static const AVOption mpeg1_options[] = { | ||
1185 | COMMON_OPTS | ||
1186 | FF_MPV_COMMON_OPTS | ||
1187 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
1188 | { NULL }, | ||
1189 | }; | ||
1190 | |||
1191 | static const AVOption mpeg2_options[] = { | ||
1192 | COMMON_OPTS | ||
1193 | { "intra_vlc", "Use MPEG-2 intra VLC table.", | ||
1194 | FF_MPV_OFFSET(intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1195 | { "non_linear_quant", "Use nonlinear quantizer.", FF_MPV_OFFSET(q_scale_type), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1196 | { "alternate_scan", "Enable alternate scantable.", FF_MPV_OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1197 | { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, | ||
1198 | { "seq_disp_ext", "Write sequence_display_extension blocks.", OFFSET(seq_disp_ext), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, .unit = "seq_disp_ext" }, | ||
1199 | { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, .unit = "seq_disp_ext" }, | ||
1200 | { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, .unit = "seq_disp_ext" }, | ||
1201 | { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, .unit = "seq_disp_ext" }, | ||
1202 | { "video_format", "Video_format in the sequence_display_extension indicating the source of the video.", OFFSET(video_format), AV_OPT_TYPE_INT, { .i64 = VIDEO_FORMAT_UNSPECIFIED }, 0, 7, VE, .unit = "video_format" }, | ||
1203 | { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, .unit = "video_format" }, | ||
1204 | { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, .unit = "video_format" }, | ||
1205 | { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, .unit = "video_format" }, | ||
1206 | { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, .unit = "video_format" }, | ||
1207 | { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, .unit = "video_format" }, | ||
1208 | { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, .unit = "video_format" }, | ||
1209 | #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, .unit = "avctx.level" | ||
1210 | { LEVEL("high", 4) }, | ||
1211 | { LEVEL("high1440", 6) }, | ||
1212 | { LEVEL("main", 8) }, | ||
1213 | { LEVEL("low", 10) }, | ||
1214 | #undef LEVEL | ||
1215 | FF_MPV_COMMON_OPTS | ||
1216 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
1217 | FF_MPEG2_PROFILE_OPTS | ||
1218 | { NULL }, | ||
1219 | }; | ||
1220 | |||
1221 | #define mpeg12_class(x) \ | ||
1222 | static const AVClass mpeg ## x ## _class = { \ | ||
1223 | .class_name = "mpeg" # x "video encoder", \ | ||
1224 | .item_name = av_default_item_name, \ | ||
1225 | .option = mpeg ## x ## _options, \ | ||
1226 | .version = LIBAVUTIL_VERSION_INT, \ | ||
1227 | }; | ||
1228 | |||
1229 | mpeg12_class(1) | ||
1230 | mpeg12_class(2) | ||
1231 | |||
1232 | const FFCodec ff_mpeg1video_encoder = { | ||
1233 | .p.name = "mpeg1video", | ||
1234 | CODEC_LONG_NAME("MPEG-1 video"), | ||
1235 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1236 | .p.id = AV_CODEC_ID_MPEG1VIDEO, | ||
1237 | .priv_data_size = sizeof(MPEG12EncContext), | ||
1238 | .init = encode_init, | ||
1239 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
1240 | .close = ff_mpv_encode_end, | ||
1241 | .p.supported_framerates = ff_mpeg12_frame_rate_tab + 1, | ||
1242 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, | ||
1243 | AV_PIX_FMT_NONE }, | ||
1244 | .color_ranges = AVCOL_RANGE_MPEG, | ||
1245 | .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | | ||
1246 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1247 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1248 | .p.priv_class = &mpeg1_class, | ||
1249 | }; | ||
1250 | |||
1251 | const FFCodec ff_mpeg2video_encoder = { | ||
1252 | .p.name = "mpeg2video", | ||
1253 | CODEC_LONG_NAME("MPEG-2 video"), | ||
1254 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1255 | .p.id = AV_CODEC_ID_MPEG2VIDEO, | ||
1256 | .priv_data_size = sizeof(MPEG12EncContext), | ||
1257 | .init = encode_init, | ||
1258 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
1259 | .close = ff_mpv_encode_end, | ||
1260 | .p.supported_framerates = ff_mpeg2_frame_rate_tab, | ||
1261 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, | ||
1262 | AV_PIX_FMT_YUV422P, | ||
1263 | AV_PIX_FMT_NONE }, | ||
1264 | .color_ranges = AVCOL_RANGE_MPEG, | ||
1265 | .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | | ||
1266 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1267 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1268 | .p.priv_class = &mpeg2_class, | ||
1269 | }; | ||
1270 | #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */ | ||
1271 |