de485cf05e5999c4f47b5f4ea34c7b7523d7e8b4
[mesa.git] / src / gallium / drivers / radeon / radeon_vcn_enc.c
1 /**************************************************************************
2 *
3 * Copyright 2017 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29
30 #include "pipe/p_video_codec.h"
31
32 #include "util/u_video.h"
33 #include "util/u_memory.h"
34
35 #include "vl/vl_video_buffer.h"
36
37 #include "r600_pipe_common.h"
38 #include "radeon_video.h"
39 #include "radeon_vcn_enc.h"
40
41 static void radeon_vcn_enc_get_param(struct radeon_encoder *enc, struct pipe_h264_enc_picture_desc *pic)
42 {
43 enc->enc_pic.picture_type = pic->picture_type;
44 enc->enc_pic.frame_num = pic->frame_num;
45 enc->enc_pic.pic_order_cnt = pic->pic_order_cnt;
46 enc->enc_pic.pic_order_cnt_type = pic->pic_order_cnt_type;
47 enc->enc_pic.ref_idx_l0 = pic->ref_idx_l0;
48 enc->enc_pic.ref_idx_l1 = pic->ref_idx_l1;
49 enc->enc_pic.not_referenced = pic->not_referenced;
50 enc->enc_pic.is_idr = pic->is_idr;
51 enc->enc_pic.crop_left = 0;
52 enc->enc_pic.crop_right = (align(enc->base.width, 16) - enc->base.width) / 2;
53 enc->enc_pic.crop_top = 0;
54 enc->enc_pic.crop_bottom = (align(enc->base.height, 16) - enc->base.height) / 2;
55 }
56
57 static void flush(struct radeon_encoder *enc)
58 {
59 enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, NULL);
60 }
61
62 static void radeon_enc_flush(struct pipe_video_codec *encoder)
63 {
64 struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
65 flush(enc);
66 }
67
68 static void radeon_enc_cs_flush(void *ctx, unsigned flags,
69 struct pipe_fence_handle **fence)
70 {
71 // just ignored
72 }
73
74 static unsigned get_cpb_num(struct radeon_encoder *enc)
75 {
76 unsigned w = align(enc->base.width, 16) / 16;
77 unsigned h = align(enc->base.height, 16) / 16;
78 unsigned dpb;
79
80 switch (enc->base.level) {
81 case 10:
82 dpb = 396;
83 break;
84 case 11:
85 dpb = 900;
86 break;
87 case 12:
88 case 13:
89 case 20:
90 dpb = 2376;
91 break;
92 case 21:
93 dpb = 4752;
94 break;
95 case 22:
96 case 30:
97 dpb = 8100;
98 break;
99 case 31:
100 dpb = 18000;
101 break;
102 case 32:
103 dpb = 20480;
104 break;
105 case 40:
106 case 41:
107 dpb = 32768;
108 break;
109 case 42:
110 dpb = 34816;
111 break;
112 case 50:
113 dpb = 110400;
114 break;
115 default:
116 case 51:
117 case 52:
118 dpb = 184320;
119 break;
120 }
121
122 return MIN2(dpb / (w * h), 16);
123 }
124
125 static void radeon_enc_begin_frame(struct pipe_video_codec *encoder,
126 struct pipe_video_buffer *source,
127 struct pipe_picture_desc *picture)
128 {
129 struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
130 struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
131 struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture;
132
133 radeon_vcn_enc_get_param(enc, pic);
134
135 enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
136 enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);
137
138 enc->need_feedback = false;
139
140 if (!enc->stream_handle) {
141 struct rvid_buffer fb;
142 enc->stream_handle = si_vid_alloc_stream_handle();
143 enc->si = CALLOC_STRUCT(rvid_buffer);
144 si_vid_create_buffer(enc->screen, enc->si, 128 * 1024, PIPE_USAGE_STAGING);
145 si_vid_create_buffer(enc->screen, &fb, 4096, PIPE_USAGE_STAGING);
146 enc->fb = &fb;
147 enc->begin(enc, pic);
148 flush(enc);
149 si_vid_destroy_buffer(&fb);
150 }
151 }
152
153 static void radeon_enc_encode_bitstream(struct pipe_video_codec *encoder,
154 struct pipe_video_buffer *source,
155 struct pipe_resource *destination,
156 void **fb)
157 {
158 struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
159 enc->get_buffer(destination, &enc->bs_handle, NULL);
160 enc->bs_size = destination->width0;
161
162 *fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
163
164 if (!si_vid_create_buffer(enc->screen, enc->fb, 4096, PIPE_USAGE_STAGING)) {
165 RVID_ERR("Can't create feedback buffer.\n");
166 return;
167 }
168
169 enc->need_feedback = true;
170 enc->encode(enc);
171 }
172
173 static void radeon_enc_end_frame(struct pipe_video_codec *encoder,
174 struct pipe_video_buffer *source,
175 struct pipe_picture_desc *picture)
176 {
177 struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
178 flush(enc);
179 }
180
181 static void radeon_enc_destroy(struct pipe_video_codec *encoder)
182 {
183 struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
184
185 if (enc->stream_handle) {
186 struct rvid_buffer fb;
187 enc->need_feedback = false;
188 si_vid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
189 enc->fb = &fb;
190 enc->destroy(enc);
191 flush(enc);
192 si_vid_destroy_buffer(&fb);
193 }
194
195 si_vid_destroy_buffer(&enc->cpb);
196 enc->ws->cs_destroy(enc->cs);
197 FREE(enc);
198 }
199
200 static void radeon_enc_get_feedback(struct pipe_video_codec *encoder,
201 void *feedback, unsigned *size)
202 {
203 struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
204 struct rvid_buffer *fb = feedback;
205
206 if (size) {
207 uint32_t *ptr = enc->ws->buffer_map(fb->res->buf, enc->cs, PIPE_TRANSFER_READ_WRITE);
208 if (ptr[1])
209 *size = ptr[6];
210 else
211 *size = 0;
212 enc->ws->buffer_unmap(fb->res->buf);
213 }
214
215 si_vid_destroy_buffer(fb);
216 FREE(fb);
217 }
218
219 struct pipe_video_codec *radeon_create_encoder(struct pipe_context *context,
220 const struct pipe_video_codec *templ,
221 struct radeon_winsys* ws,
222 radeon_enc_get_buffer get_buffer)
223 {
224 /* TODO*/
225 }