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