aboutsummaryrefslogtreecommitdiff
path: root/webrtc/common_video/libyuv/webrtc_libyuv.cc
blob: bf9562476944b55784ac98094cd36fde30bea85d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"

#include <assert.h>
#include <string.h>

// NOTE(ajm): Path provided by gyp.
#include "libyuv.h"  // NOLINT

namespace webrtc {

const int k16ByteAlignment = 16;

VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type) {
  switch (type) {
    case kVideoI420:
      return kI420;
    case kVideoIYUV:
      return kIYUV;
    case kVideoRGB24:
      return kRGB24;
    case kVideoARGB:
      return kARGB;
    case kVideoARGB4444:
      return kARGB4444;
    case kVideoRGB565:
      return kRGB565;
    case kVideoARGB1555:
      return kARGB1555;
    case kVideoYUY2:
      return kYUY2;
    case kVideoYV12:
      return kYV12;
    case kVideoUYVY:
      return kUYVY;
    case kVideoNV21:
      return kNV21;
    case kVideoNV12:
      return kNV12;
    case kVideoBGRA:
      return kBGRA;
    case kVideoMJPEG:
      return kMJPG;
    default:
      assert(false);
  }
  return kUnknown;
}

int AlignInt(int value, int alignment) {
  assert(!((alignment - 1) & alignment));
  return ((value + alignment - 1) & ~ (alignment - 1));
}

void Calc16ByteAlignedStride(int width, int* stride_y, int* stride_uv) {
  *stride_y = AlignInt(width, k16ByteAlignment);
  *stride_uv = AlignInt((width + 1) / 2, k16ByteAlignment);
}

size_t CalcBufferSize(VideoType type, int width, int height) {
  assert(width >= 0);
  assert(height >= 0);
  size_t buffer_size = 0;
  switch (type) {
    case kI420:
    case kNV12:
    case kNV21:
    case kIYUV:
    case kYV12: {
      int half_width = (width + 1) >> 1;
      int half_height = (height + 1) >> 1;
      buffer_size = width * height + half_width * half_height * 2;
      break;
    }
    case kARGB4444:
    case kRGB565:
    case kARGB1555:
    case kYUY2:
    case kUYVY:
      buffer_size = width * height * 2;
      break;
    case kRGB24:
      buffer_size = width * height * 3;
      break;
    case kBGRA:
    case kARGB:
      buffer_size = width * height * 4;
      break;
    default:
      assert(false);
      break;
  }
  return buffer_size;
}

int PrintVideoFrame(const VideoFrame& frame, FILE* file) {
  if (file == NULL)
    return -1;
  if (frame.IsZeroSize())
    return -1;
  for (int planeNum = 0; planeNum < kNumOfPlanes; ++planeNum) {
    int width = (planeNum ? (frame.width() + 1) / 2 : frame.width());
    int height = (planeNum ? (frame.height() + 1) / 2 : frame.height());
    PlaneType plane_type = static_cast<PlaneType>(planeNum);
    const uint8_t* plane_buffer = frame.buffer(plane_type);
    for (int y = 0; y < height; y++) {
     if (fwrite(plane_buffer, 1, width, file) !=
         static_cast<unsigned int>(width)) {
       return -1;
       }
       plane_buffer += frame.stride(plane_type);
    }
 }
 return 0;
}

int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer) {
  assert(buffer);
  if (input_frame.IsZeroSize())
    return -1;
  size_t length =
      CalcBufferSize(kI420, input_frame.width(), input_frame.height());
  if (size < length) {
     return -1;
  }

  int pos = 0;
  uint8_t* buffer_ptr = buffer;

  for (int plane = 0; plane < kNumOfPlanes; ++plane) {
    int width = (plane ? (input_frame.width() + 1) / 2 :
      input_frame.width());
    int height = (plane ? (input_frame.height() + 1) / 2 :
      input_frame.height());
    const uint8_t* plane_ptr = input_frame.buffer(
        static_cast<PlaneType>(plane));
    for (int y = 0; y < height; y++) {
      memcpy(&buffer_ptr[pos], plane_ptr, width);
      pos += width;
      plane_ptr += input_frame.stride(static_cast<PlaneType>(plane));
    }
  }
  return static_cast<int>(length);
}


int ConvertNV12ToRGB565(const uint8_t* src_frame,
                        uint8_t* dst_frame,
                        int width, int height) {
  int abs_height = (height < 0) ? -height : height;
  const uint8_t* yplane = src_frame;
  const uint8_t* uvInterlaced = src_frame + (width * abs_height);

  return libyuv::NV12ToRGB565(yplane, width,
                              uvInterlaced, (width + 1) >> 1,
                              dst_frame, width,
                              width, height);
}

int ConvertRGB24ToARGB(const uint8_t* src_frame, uint8_t* dst_frame,
                       int width, int height, int dst_stride) {
  if (dst_stride == 0)
    dst_stride = width;
  return libyuv::RGB24ToARGB(src_frame, width,
                             dst_frame, dst_stride,
                             width, height);
}

libyuv::RotationMode ConvertRotationMode(VideoRotation rotation) {
  switch(rotation) {
    case kVideoRotation_0:
      return libyuv::kRotate0;
    case kVideoRotation_90:
      return libyuv::kRotate90;
    case kVideoRotation_180:
      return libyuv::kRotate180;
    case kVideoRotation_270:
      return libyuv::kRotate270;
  }
  assert(false);
  return libyuv::kRotate0;
}

int ConvertVideoType(VideoType video_type) {
  switch(video_type) {
    case kUnknown:
      return libyuv::FOURCC_ANY;
    case  kI420:
      return libyuv::FOURCC_I420;
    case kIYUV:  // same as KYV12
    case kYV12:
      return libyuv::FOURCC_YV12;
    case kRGB24:
      return libyuv::FOURCC_24BG;
    case kABGR:
      return libyuv::FOURCC_ABGR;
    case kRGB565:
      return libyuv::FOURCC_RGBP;
    case kYUY2:
      return libyuv::FOURCC_YUY2;
    case kUYVY:
      return libyuv::FOURCC_UYVY;
    case kMJPG:
      return libyuv::FOURCC_MJPG;
    case kNV21:
      return libyuv::FOURCC_NV21;
    case kNV12:
      return libyuv::FOURCC_NV12;
    case kARGB:
      return libyuv::FOURCC_ARGB;
    case kBGRA:
      return libyuv::FOURCC_BGRA;
    case kARGB4444:
      return libyuv::FOURCC_R444;
    case kARGB1555:
      return libyuv::FOURCC_RGBO;
  }
  assert(false);
  return libyuv::FOURCC_ANY;
}

int ConvertToI420(VideoType src_video_type,
                  const uint8_t* src_frame,
                  int crop_x,
                  int crop_y,
                  int src_width,
                  int src_height,
                  size_t sample_size,
                  VideoRotation rotation,
                  VideoFrame* dst_frame) {
  int dst_width = dst_frame->width();
  int dst_height = dst_frame->height();
  // LibYuv expects pre-rotation values for dst.
  // Stride values should correspond to the destination values.
  if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) {
    dst_width = dst_frame->height();
    dst_height =dst_frame->width();
  }
  return libyuv::ConvertToI420(src_frame, sample_size,
                               dst_frame->buffer(kYPlane),
                               dst_frame->stride(kYPlane),
                               dst_frame->buffer(kUPlane),
                               dst_frame->stride(kUPlane),
                               dst_frame->buffer(kVPlane),
                               dst_frame->stride(kVPlane),
                               crop_x, crop_y,
                               src_width, src_height,
                               dst_width, dst_height,
                               ConvertRotationMode(rotation),
                               ConvertVideoType(src_video_type));
}

int ConvertFromI420(const VideoFrame& src_frame,
                    VideoType dst_video_type,
                    int dst_sample_size,
                    uint8_t* dst_frame) {
  return libyuv::ConvertFromI420(src_frame.buffer(kYPlane),
                                 src_frame.stride(kYPlane),
                                 src_frame.buffer(kUPlane),
                                 src_frame.stride(kUPlane),
                                 src_frame.buffer(kVPlane),
                                 src_frame.stride(kVPlane),
                                 dst_frame, dst_sample_size,
                                 src_frame.width(), src_frame.height(),
                                 ConvertVideoType(dst_video_type));
}

// TODO(mikhal): Create a designated VideoFrame for non I420.
int ConvertFromYV12(const VideoFrame& src_frame,
                    VideoType dst_video_type,
                    int dst_sample_size,
                    uint8_t* dst_frame) {
  // YV12 = Y, V, U
  return libyuv::ConvertFromI420(src_frame.buffer(kYPlane),
                                 src_frame.stride(kYPlane),
                                 src_frame.buffer(kVPlane),
                                 src_frame.stride(kVPlane),
                                 src_frame.buffer(kUPlane),
                                 src_frame.stride(kUPlane),
                                 dst_frame, dst_sample_size,
                                 src_frame.width(), src_frame.height(),
                                 ConvertVideoType(dst_video_type));
}

// Compute PSNR for an I420 frame (all planes)
double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
  if (!ref_frame || !test_frame)
    return -1;
  else if ((ref_frame->width() !=  test_frame->width()) ||
          (ref_frame->height() !=  test_frame->height()))
    return -1;
  else if (ref_frame->width() < 0 || ref_frame->height() < 0)
    return -1;

  double psnr = libyuv::I420Psnr(ref_frame->buffer(kYPlane),
                                 ref_frame->stride(kYPlane),
                                 ref_frame->buffer(kUPlane),
                                 ref_frame->stride(kUPlane),
                                 ref_frame->buffer(kVPlane),
                                 ref_frame->stride(kVPlane),
                                 test_frame->buffer(kYPlane),
                                 test_frame->stride(kYPlane),
                                 test_frame->buffer(kUPlane),
                                 test_frame->stride(kUPlane),
                                 test_frame->buffer(kVPlane),
                                 test_frame->stride(kVPlane),
                                 test_frame->width(), test_frame->height());
  // LibYuv sets the max psnr value to 128, we restrict it here.
  // In case of 0 mse in one frame, 128 can skew the results significantly.
  return (psnr > kPerfectPSNR) ? kPerfectPSNR : psnr;
}

// Compute SSIM for an I420 frame (all planes)
double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
  if (!ref_frame || !test_frame)
    return -1;
  else if ((ref_frame->width() !=  test_frame->width()) ||
          (ref_frame->height() !=  test_frame->height()))
    return -1;
  else if (ref_frame->width() < 0 || ref_frame->height()  < 0)
    return -1;

  return libyuv::I420Ssim(ref_frame->buffer(kYPlane),
                          ref_frame->stride(kYPlane),
                          ref_frame->buffer(kUPlane),
                          ref_frame->stride(kUPlane),
                          ref_frame->buffer(kVPlane),
                          ref_frame->stride(kVPlane),
                          test_frame->buffer(kYPlane),
                          test_frame->stride(kYPlane),
                          test_frame->buffer(kUPlane),
                          test_frame->stride(kUPlane),
                          test_frame->buffer(kVPlane),
                          test_frame->stride(kVPlane),
                          test_frame->width(), test_frame->height());
}
}  // namespace webrtc