]> git.sesse.net Git - nageru/blob - ycbcr_converter.cpp
Stop leaking VA-API buffers.
[nageru] / ycbcr_converter.cpp
1 #include "ycbcr_converter.h"
2
3 #include "jpeg_frame.h"
4
5 #include <movit/mix_effect.h>
6 #include <movit/ycbcr_input.h>
7
8 using namespace std;
9 using namespace movit;
10
11 namespace {
12
13 void setup_outputs(YCbCrConverter::OutputMode output_mode, const ImageFormat &output_format, const YCbCrFormat &ycbcr_output_format, EffectChain *chain)
14 {
15         if (output_mode == YCbCrConverter::OUTPUT_TO_RGBA) {
16                 chain->add_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
17                 chain->set_output_origin(OUTPUT_ORIGIN_BOTTOM_LEFT);
18         } else if (output_mode == YCbCrConverter::OUTPUT_TO_SEMIPLANAR) {
19                 chain->add_ycbcr_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_output_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
20                 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
21         } else {
22                 assert(output_mode == YCbCrConverter::OUTPUT_TO_DUAL_YCBCR);
23
24                 // One full Y'CbCr texture (for interpolation), one that's just Y (throwing away the
25                 // Cb and Cr channels). The second copy is sort of redundant, but it's the easiest way
26                 // of getting the gray data into a layered texture.
27                 chain->add_ycbcr_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_output_format);
28                 chain->add_ycbcr_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_output_format);
29                 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
30         }
31 }
32
33 }  // namespace
34
35 YCbCrConverter::YCbCrConverter(YCbCrConverter::OutputMode output_mode, ResourcePool *resource_pool)
36 {
37         ImageFormat inout_format;
38         inout_format.color_space = COLORSPACE_sRGB;
39         inout_format.gamma_curve = GAMMA_sRGB;
40
41         ycbcr_format.luma_coefficients = YCBCR_REC_709;
42         ycbcr_format.full_range = false;
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;
50
51         YCbCrFormat ycbcr_output_format = ycbcr_format;
52         ycbcr_output_format.chroma_subsampling_x = 1;
53
54         // Planar Y'CbCr decoding chain.
55         planar_chain.reset(new EffectChain(1280, 720, resource_pool));
56         ycbcr_planar_input = (YCbCrInput *)planar_chain->add_input(new YCbCrInput(inout_format, ycbcr_format, 1280, 720, YCBCR_INPUT_PLANAR));
57         setup_outputs(output_mode, inout_format, ycbcr_output_format, planar_chain.get());
58         planar_chain->set_dither_bits(8);
59         planar_chain->finalize();
60
61         // Semiplanar Y'CbCr decoding chain (for images coming from VA-API).
62         semiplanar_chain.reset(new EffectChain(1280, 720, resource_pool));
63         ycbcr_semiplanar_input = (YCbCrInput *)semiplanar_chain->add_input(new YCbCrInput(inout_format, ycbcr_format, 1280, 720, YCBCR_INPUT_SPLIT_Y_AND_CBCR));
64         setup_outputs(output_mode, inout_format, ycbcr_output_format, semiplanar_chain.get());
65         semiplanar_chain->set_dither_bits(8);
66         semiplanar_chain->finalize();
67
68         // Fade chains.
69         for (bool first_input_is_semiplanar : { false, true }) {
70                 for (bool second_input_is_semiplanar : { false, true }) {
71                         FadeChain &fade_chain = fade_chains[first_input_is_semiplanar][second_input_is_semiplanar];
72                         fade_chain.chain.reset(new EffectChain(1280, 720, resource_pool));
73                         fade_chain.input[0] = (movit::YCbCrInput *)fade_chain.chain->add_input(
74                                 new YCbCrInput(inout_format, ycbcr_format, 1280, 720,
75                                         first_input_is_semiplanar ? YCBCR_INPUT_SPLIT_Y_AND_CBCR : YCBCR_INPUT_PLANAR));
76                         fade_chain.input[1] = (movit::YCbCrInput *)fade_chain.chain->add_input(
77                                 new YCbCrInput(inout_format, ycbcr_format, 1280, 720,
78                                         second_input_is_semiplanar ? YCBCR_INPUT_SPLIT_Y_AND_CBCR : YCBCR_INPUT_PLANAR));
79                         fade_chain.mix_effect = (movit::MixEffect *)fade_chain.chain->add_effect(
80                                 new MixEffect, fade_chain.input[0], fade_chain.input[1]);
81                         setup_outputs(output_mode, inout_format, ycbcr_output_format, fade_chain.chain.get());
82                         fade_chain.chain->set_dither_bits(8);
83                         fade_chain.chain->finalize();
84                 }
85         }
86
87         // Fade from interleaved chain (ie., first input is interleaved, since it comes
88         // directly from the GPU anyway).
89         for (bool second_input_is_semiplanar : { false, true }) {
90                 FadeChain &fade_chain = interleaved_fade_chains[second_input_is_semiplanar];
91                 fade_chain.chain.reset(new EffectChain(1280, 720, resource_pool));
92
93                 ycbcr_format.chroma_subsampling_x = 1;
94                 fade_chain.input[0] = (movit::YCbCrInput *)fade_chain.chain->add_input(
95                         new YCbCrInput(inout_format, ycbcr_format, 1280, 720,
96                                 YCBCR_INPUT_INTERLEAVED));
97
98                 ycbcr_format.chroma_subsampling_x = 2;
99                 fade_chain.input[1] = (movit::YCbCrInput *)fade_chain.chain->add_input(
100                         new YCbCrInput(inout_format, ycbcr_format, 1280, 720,
101                                 second_input_is_semiplanar ? YCBCR_INPUT_SPLIT_Y_AND_CBCR : YCBCR_INPUT_PLANAR));
102
103                 fade_chain.mix_effect = (movit::MixEffect *)fade_chain.chain->add_effect(
104                         new MixEffect, fade_chain.input[0], fade_chain.input[1]);
105                 setup_outputs(output_mode, inout_format, ycbcr_output_format, fade_chain.chain.get());
106                 fade_chain.chain->set_dither_bits(8);
107                 fade_chain.chain->finalize();
108         }
109 }
110
111 EffectChain *YCbCrConverter::prepare_chain_for_conversion(shared_ptr<Frame> frame)
112 {
113         if (frame->is_semiplanar) {
114                 setup_input_for_frame(frame, ycbcr_format, ycbcr_semiplanar_input);
115                 return semiplanar_chain.get();
116         } else {
117                 setup_input_for_frame(frame, ycbcr_format, ycbcr_planar_input);
118                 return planar_chain.get();
119         }
120 }
121
122 EffectChain *YCbCrConverter::prepare_chain_for_fade(shared_ptr<Frame> frame, shared_ptr<Frame> secondary_frame, float fade_alpha)
123 {
124         const FadeChain &fade_chain = fade_chains[frame->is_semiplanar][secondary_frame->is_semiplanar];
125         setup_input_for_frame(frame, ycbcr_format, fade_chain.input[0]);
126         setup_input_for_frame(secondary_frame, ycbcr_format, fade_chain.input[1]);
127         bool ok = fade_chain.mix_effect->set_float("strength_first", 1.0f - fade_alpha);
128         ok |= fade_chain.mix_effect->set_float("strength_second", fade_alpha);
129         assert(ok);
130         return fade_chain.chain.get();
131 }
132
133 EffectChain *YCbCrConverter::prepare_chain_for_fade_from_texture(GLuint tex, std::shared_ptr<Frame> secondary_frame, float fade_alpha)
134 {
135         const FadeChain &fade_chain = interleaved_fade_chains[secondary_frame->is_semiplanar];
136         {
137                 YCbCrFormat format_copy = ycbcr_format;
138                 format_copy.chroma_subsampling_x = 1;
139                 format_copy.chroma_subsampling_y = 1;
140                 fade_chain.input[0]->change_ycbcr_format(format_copy);
141
142                 fade_chain.input[0]->set_width(1280);  // FIXME
143                 fade_chain.input[0]->set_height(720);
144                 fade_chain.input[0]->set_texture_num(0, tex);
145
146                 glTextureParameteri(tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
147                 glTextureParameteri(tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
148                 glTextureParameteri(tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
149                 glTextureParameteri(tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
150         }
151         setup_input_for_frame(secondary_frame, ycbcr_format, fade_chain.input[1]);
152         bool ok = fade_chain.mix_effect->set_float("strength_first", 1.0f - fade_alpha);
153         ok |= fade_chain.mix_effect->set_float("strength_second", fade_alpha);
154         assert(ok);
155         return fade_chain.chain.get();
156 }
157
158 void setup_input_for_frame(shared_ptr<Frame> frame, const YCbCrFormat &ycbcr_format, YCbCrInput *input)
159 {
160         YCbCrFormat format_copy = ycbcr_format;
161         format_copy.chroma_subsampling_x = frame->chroma_subsampling_x;
162         format_copy.chroma_subsampling_y = frame->chroma_subsampling_y;
163         input->change_ycbcr_format(format_copy);
164
165         input->set_width(frame->width);
166         input->set_height(frame->height);
167         input->set_pixel_data(0, frame->y.get());
168         input->set_pitch(0, frame->pitch_y);
169         if (frame->is_semiplanar) {
170                 input->set_pixel_data(1, frame->cbcr.get());
171                 input->set_pitch(1, frame->pitch_chroma);
172         } else {
173                 input->set_pixel_data(1, frame->cb.get());
174                 input->set_pixel_data(2, frame->cr.get());
175                 input->set_pitch(1, frame->pitch_chroma);
176                 input->set_pitch(2, frame->pitch_chroma);
177         }
178 }