1 #include "ycbcr_converter.h"
4 #include "jpeg_frame.h"
6 #include <movit/mix_effect.h>
7 #include <movit/ycbcr_input.h>
10 using namespace movit;
14 void setup_outputs(YCbCrConverter::OutputMode output_mode, const ImageFormat &output_format, const YCbCrFormat &ycbcr_output_format, EffectChain *chain)
16 if (output_mode == YCbCrConverter::OUTPUT_TO_RGBA) {
17 chain->add_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
18 chain->set_output_origin(OUTPUT_ORIGIN_BOTTOM_LEFT);
19 } else if (output_mode == YCbCrConverter::OUTPUT_TO_SEMIPLANAR) {
20 chain->add_ycbcr_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_output_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
21 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
23 assert(output_mode == YCbCrConverter::OUTPUT_TO_DUAL_YCBCR);
25 // One full Y'CbCr texture (for interpolation), one that's just Y (throwing away the
26 // Cb and Cr channels). The second copy is sort of redundant, but it's the easiest way
27 // of getting the gray data into a layered texture.
28 chain->add_ycbcr_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_output_format);
29 chain->add_ycbcr_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_output_format);
30 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
36 YCbCrConverter::YCbCrConverter(YCbCrConverter::OutputMode output_mode, ResourcePool *resource_pool)
38 ImageFormat inout_format;
39 inout_format.color_space = COLORSPACE_sRGB;
40 inout_format.gamma_curve = GAMMA_sRGB;
42 ycbcr_format.luma_coefficients = YCBCR_REC_709;
43 ycbcr_format.num_levels = 256;
44 ycbcr_format.chroma_subsampling_x = 2;
45 ycbcr_format.chroma_subsampling_y = 1;
46 ycbcr_format.cb_x_position = 0.0f; // H.264 -- _not_ JPEG, even though our input is MJPEG-encoded
47 ycbcr_format.cb_y_position = 0.5f; // Irrelevant.
48 ycbcr_format.cr_x_position = 0.0f;
49 ycbcr_format.cr_y_position = 0.5f;
51 // This is a hack. Even though we're sending MJPEG around, which is
52 // full-range, it's mostly transporting signals from limited-range
53 // sources with no conversion, so we ought to have had false here.
54 // However, in the off chance that we're actually getting real MJPEG,
55 // we don't want to crush its blacks (or whites) by clamping. All of
56 // our processing is fades, so if we're in limited-range input, we'll
57 // stay in limited-range output. (Fading between limited-range and
58 // full-range sources will be broken, of course.) There will be some
59 // slight confusion in the parts of the algorithms dealing with RGB,
60 // but they're small and we'll manage.
61 ycbcr_format.full_range = true;
63 YCbCrFormat ycbcr_output_format = ycbcr_format;
64 ycbcr_output_format.chroma_subsampling_x = 1;
66 // Planar Y'CbCr decoding chain.
67 planar_chain.reset(new EffectChain(global_flags.width, global_flags.height, resource_pool));
68 ycbcr_planar_input = (YCbCrInput *)planar_chain->add_input(new YCbCrInput(inout_format, ycbcr_format, global_flags.width, global_flags.height, YCBCR_INPUT_PLANAR));
69 setup_outputs(output_mode, inout_format, ycbcr_output_format, planar_chain.get());
70 planar_chain->set_dither_bits(8);
71 planar_chain->finalize();
73 // Semiplanar Y'CbCr decoding chain (for images coming from VA-API).
74 semiplanar_chain.reset(new EffectChain(global_flags.width, global_flags.height, resource_pool));
75 ycbcr_semiplanar_input = (YCbCrInput *)semiplanar_chain->add_input(new YCbCrInput(inout_format, ycbcr_format, global_flags.width, global_flags.height, YCBCR_INPUT_SPLIT_Y_AND_CBCR));
76 setup_outputs(output_mode, inout_format, ycbcr_output_format, semiplanar_chain.get());
77 semiplanar_chain->set_dither_bits(8);
78 semiplanar_chain->finalize();
81 for (bool first_input_is_semiplanar : { false, true }) {
82 for (bool second_input_is_semiplanar : { false, true }) {
83 FadeChain &fade_chain = fade_chains[first_input_is_semiplanar][second_input_is_semiplanar];
84 fade_chain.chain.reset(new EffectChain(global_flags.width, global_flags.height, resource_pool));
85 fade_chain.input[0] = (movit::YCbCrInput *)fade_chain.chain->add_input(
86 new YCbCrInput(inout_format, ycbcr_format, global_flags.width, global_flags.height,
87 first_input_is_semiplanar ? YCBCR_INPUT_SPLIT_Y_AND_CBCR : YCBCR_INPUT_PLANAR));
88 fade_chain.input[1] = (movit::YCbCrInput *)fade_chain.chain->add_input(
89 new YCbCrInput(inout_format, ycbcr_format, global_flags.width, global_flags.height,
90 second_input_is_semiplanar ? YCBCR_INPUT_SPLIT_Y_AND_CBCR : YCBCR_INPUT_PLANAR));
91 fade_chain.mix_effect = (movit::MixEffect *)fade_chain.chain->add_effect(
92 new MixEffect, fade_chain.input[0], fade_chain.input[1]);
93 setup_outputs(output_mode, inout_format, ycbcr_output_format, fade_chain.chain.get());
94 fade_chain.chain->set_dither_bits(8);
95 fade_chain.chain->finalize();
99 // Fade from interleaved chain (ie., first input is interleaved, since it comes
100 // directly from the GPU anyway).
101 for (bool second_input_is_semiplanar : { false, true }) {
102 FadeChain &fade_chain = interleaved_fade_chains[second_input_is_semiplanar];
103 fade_chain.chain.reset(new EffectChain(global_flags.width, global_flags.height, resource_pool));
105 ycbcr_format.chroma_subsampling_x = 1;
106 fade_chain.input[0] = (movit::YCbCrInput *)fade_chain.chain->add_input(
107 new YCbCrInput(inout_format, ycbcr_format, global_flags.width, global_flags.height,
108 YCBCR_INPUT_INTERLEAVED));
110 ycbcr_format.chroma_subsampling_x = 2;
111 fade_chain.input[1] = (movit::YCbCrInput *)fade_chain.chain->add_input(
112 new YCbCrInput(inout_format, ycbcr_format, global_flags.width, global_flags.height,
113 second_input_is_semiplanar ? YCBCR_INPUT_SPLIT_Y_AND_CBCR : YCBCR_INPUT_PLANAR));
115 fade_chain.mix_effect = (movit::MixEffect *)fade_chain.chain->add_effect(
116 new MixEffect, fade_chain.input[0], fade_chain.input[1]);
117 setup_outputs(output_mode, inout_format, ycbcr_output_format, fade_chain.chain.get());
118 fade_chain.chain->set_dither_bits(8);
119 fade_chain.chain->finalize();
123 EffectChain *YCbCrConverter::prepare_chain_for_conversion(shared_ptr<Frame> frame)
125 if (frame->is_semiplanar) {
126 setup_input_for_frame(frame, ycbcr_format, ycbcr_semiplanar_input);
127 return semiplanar_chain.get();
129 setup_input_for_frame(frame, ycbcr_format, ycbcr_planar_input);
130 return planar_chain.get();
134 EffectChain *YCbCrConverter::prepare_chain_for_fade(shared_ptr<Frame> frame, shared_ptr<Frame> secondary_frame, float fade_alpha)
136 const FadeChain &fade_chain = fade_chains[frame->is_semiplanar][secondary_frame->is_semiplanar];
137 setup_input_for_frame(frame, ycbcr_format, fade_chain.input[0]);
138 setup_input_for_frame(secondary_frame, ycbcr_format, fade_chain.input[1]);
139 bool ok = fade_chain.mix_effect->set_float("strength_first", 1.0f - fade_alpha);
140 ok |= fade_chain.mix_effect->set_float("strength_second", fade_alpha);
142 return fade_chain.chain.get();
145 EffectChain *YCbCrConverter::prepare_chain_for_fade_from_texture(GLuint tex, unsigned width, unsigned height, std::shared_ptr<Frame> secondary_frame, float fade_alpha)
147 const FadeChain &fade_chain = interleaved_fade_chains[secondary_frame->is_semiplanar];
149 YCbCrFormat format_copy = ycbcr_format;
150 format_copy.chroma_subsampling_x = 1;
151 format_copy.chroma_subsampling_y = 1;
152 fade_chain.input[0]->change_ycbcr_format(format_copy);
154 fade_chain.input[0]->set_width(width); // Doesn't really matter.
155 fade_chain.input[0]->set_height(height);
156 fade_chain.input[0]->set_texture_num(0, tex);
158 glTextureParameteri(tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
159 glTextureParameteri(tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
160 glTextureParameteri(tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
161 glTextureParameteri(tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
163 setup_input_for_frame(secondary_frame, ycbcr_format, fade_chain.input[1]);
164 bool ok = fade_chain.mix_effect->set_float("strength_first", 1.0f - fade_alpha);
165 ok |= fade_chain.mix_effect->set_float("strength_second", fade_alpha);
167 return fade_chain.chain.get();
170 void setup_input_for_frame(shared_ptr<Frame> frame, const YCbCrFormat &ycbcr_format, YCbCrInput *input)
172 YCbCrFormat format_copy = ycbcr_format;
173 format_copy.chroma_subsampling_x = frame->chroma_subsampling_x;
174 format_copy.chroma_subsampling_y = frame->chroma_subsampling_y;
175 input->change_ycbcr_format(format_copy);
177 input->set_width(frame->width);
178 input->set_height(frame->height);
179 input->set_pixel_data(0, frame->y.get());
180 input->set_pitch(0, frame->pitch_y);
181 if (frame->is_semiplanar) {
182 input->set_pixel_data(1, frame->cbcr.get());
183 input->set_pitch(1, frame->pitch_chroma);
185 input->set_pixel_data(1, frame->cb.get());
186 input->set_pixel_data(2, frame->cr.get());
187 input->set_pitch(1, frame->pitch_chroma);
188 input->set_pitch(2, frame->pitch_chroma);