summaryrefslogtreecommitdiffstats
path: root/media/base/yuv_convert_c.cc
blob: b5a835bc102f55b6f442c6e35df05ed1e40e45bf (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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/yuv_convert.h"
#include "media/base/yuv_convert_internal.h"

namespace media {

static int clip_byte(int x) {
  if (x > 255)
    return 255;
  else if (x < 0)
    return 0;
  else
    return x;
}

void ConvertRGB32ToYUV_C(const uint8* rgbframe,
                         uint8* yplane,
                         uint8* uplane,
                         uint8* vplane,
                         int width,
                         int height,
                         int rgbstride,
                         int ystride,
                         int uvstride) {
  for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
      // Since the input pixel format is RGB32, there are 4 bytes per pixel.
      const uint8* pixel = rgbframe + 4 * j;
      yplane[j] = clip_byte(((pixel[2] * 66 + pixel[1] * 129 +
                             pixel[0] * 25 + 128) >> 8) + 16);
      if (i % 2 == 0 && j % 2 == 0) {
        uplane[j / 2] = clip_byte(((pixel[2] * -38 + pixel[1] * -74 +
                                   pixel[0] * 112 + 128) >> 8) + 128);
        vplane[j / 2] = clip_byte(((pixel[2] * 112 + pixel[1] * -94 +
                                    pixel[1] * -18 + 128) >> 8) + 128);
      }
    }

    rgbframe += rgbstride;
    yplane += ystride;
    if (i % 2 == 0) {
      uplane += uvstride;
      vplane += uvstride;
    }
  }
}

void ConvertRGB24ToYUV_C(const uint8* rgbframe,
                         uint8* yplane,
                         uint8* uplane,
                         uint8* vplane,
                         int width,
                         int height,
                         int rgbstride,
                         int ystride,
                         int uvstride) {
  for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
      // Since the input pixel format is RGB24, there are 3 bytes per pixel.
      const uint8* pixel = rgbframe + 3 * j;
      yplane[j] = clip_byte(((pixel[2] * 66 + pixel[1] * 129 +
                              pixel[0] * 25 + 128) >> 8) + 16);
      if (i % 2 == 0 && j % 2 == 0) {
        uplane[j / 2] = clip_byte(((pixel[2] * -38 + pixel[1] * -74 +
                                    pixel[0] * 112 + 128) >> 8) + 128);
        vplane[j / 2] = clip_byte(((pixel[2] * 112 + pixel[1] * -94 +
                                    pixel[1] * -18 + 128) >> 8) + 128);
      }
    }

    rgbframe += rgbstride;
    yplane += ystride;
    if (i % 2 == 0) {
      uplane += uvstride;
      vplane += uvstride;
    }
  }
}

void ConvertYUY2ToYUV_C(const uint8* src,
                        uint8* yplane,
                        uint8* uplane,
                        uint8* vplane,
                        int width,
                        int height) {
  for (int i = 0; i < height / 2; ++i) {
    for (int j = 0; j < (width / 2); ++j) {
      yplane[0] = src[0];
      *uplane = src[1];
      yplane[1] = src[2];
      *vplane = src[3];
      src += 4;
      yplane += 2;
      uplane++;
      vplane++;
    }
    for (int j = 0; j < (width / 2); ++j) {
      yplane[0] = src[0];
      yplane[1] = src[2];
      src += 4;
      yplane += 2;
    }
  }
}

}  // namespace media