OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <math.h> | 5 #include <math.h> |
6 #include <cstdio> | 6 #include <cstdio> |
7 | 7 |
8 #include "media/base/video_frame.h" | |
9 #include "media/cast/test/video_utility.h" | 8 #include "media/cast/test/video_utility.h" |
10 #include "third_party/libyuv/include/libyuv/compare.h" | 9 #include "third_party/libyuv/include/libyuv/compare.h" |
11 #include "ui/gfx/size.h" | 10 #include "ui/gfx/size.h" |
12 | 11 |
13 namespace media { | 12 namespace media { |
14 namespace cast { | 13 namespace cast { |
15 | 14 |
16 double I420PSNR(const I420VideoFrame& frame1, const I420VideoFrame& frame2) { | 15 double I420PSNR(const scoped_refptr<media::VideoFrame>& frame1, |
17 // Frames should have equal resolution. | 16 const scoped_refptr<media::VideoFrame>& frame2) { |
18 if (frame1.width != frame2.width || frame1.height != frame2.height) return -1; | 17 if (frame1->coded_size().width() != frame2->coded_size().width() || |
19 return libyuv::I420Psnr(frame1.y_plane.data, frame1.y_plane.stride, | 18 frame1->coded_size().height() != frame2->coded_size().height()) return -1; |
20 frame1.u_plane.data, frame1.u_plane.stride, | |
21 frame1.v_plane.data, frame1.v_plane.stride, | |
22 frame2.y_plane.data, frame2.y_plane.stride, | |
23 frame2.u_plane.data, frame2.u_plane.stride, | |
24 frame2.v_plane.data, frame2.v_plane.stride, | |
25 frame1.width, frame1.height); | |
26 } | |
27 | |
28 double I420PSNR(const VideoFrame& frame1, const I420VideoFrame& frame2) { | |
29 if (frame1.coded_size().width() != frame2.width || | |
30 frame1.coded_size().height() != frame2.height) return -1; | |
31 | 19 |
32 return libyuv::I420Psnr( | 20 return libyuv::I420Psnr( |
33 frame1.data(VideoFrame::kYPlane), frame1.stride(VideoFrame::kYPlane), | 21 frame1->data(VideoFrame::kYPlane), frame1->stride(VideoFrame::kYPlane), |
34 frame1.data(VideoFrame::kUPlane), frame1.stride(VideoFrame::kUPlane), | 22 frame1->data(VideoFrame::kUPlane), frame1->stride(VideoFrame::kUPlane), |
35 frame1.data(VideoFrame::kVPlane), frame1.stride(VideoFrame::kVPlane), | 23 frame1->data(VideoFrame::kVPlane), frame1->stride(VideoFrame::kVPlane), |
36 frame2.y_plane.data, frame2.y_plane.stride, | 24 frame2->data(VideoFrame::kYPlane), frame2->stride(VideoFrame::kYPlane), |
37 frame2.u_plane.data, frame2.u_plane.stride, | 25 frame2->data(VideoFrame::kUPlane), frame2->stride(VideoFrame::kUPlane), |
38 frame2.v_plane.data, frame2.v_plane.stride, | 26 frame2->data(VideoFrame::kVPlane), frame2->stride(VideoFrame::kVPlane), |
39 frame2.width, frame2.height); | 27 frame1->coded_size().width(), frame1->coded_size().height()); |
40 } | 28 } |
41 | 29 |
42 void PopulateVideoFrame(VideoFrame* frame, int start_value) { | 30 void PopulateVideoFrame(VideoFrame* frame, int start_value) { |
43 int width = frame->coded_size().width(); | 31 int width = frame->coded_size().width(); |
44 int height = frame->coded_size().height(); | 32 int height = frame->coded_size().height(); |
45 int half_width = (width + 1) / 2; | 33 int half_width = (width + 1) / 2; |
46 int half_height = (height + 1) / 2; | 34 int half_height = (height + 1) / 2; |
47 uint8* y_plane = frame->data(VideoFrame::kYPlane); | 35 uint8* y_plane = frame->data(VideoFrame::kYPlane); |
48 uint8* u_plane = frame->data(VideoFrame::kUPlane); | 36 uint8* u_plane = frame->data(VideoFrame::kUPlane); |
49 uint8* v_plane = frame->data(VideoFrame::kVPlane); | 37 uint8* v_plane = frame->data(VideoFrame::kVPlane); |
50 | 38 |
51 // Set Y. | 39 // Set Y. |
52 for (int i = 0; i < width * height; ++i) { | 40 for (int i = 0; i < width * height; ++i) { |
53 y_plane[i] = static_cast<uint8>(start_value + i); | 41 y_plane[i] = static_cast<uint8>(start_value + i); |
54 } | 42 } |
55 | 43 |
56 // Set U. | 44 // Set U. |
57 for (int i = 0; i < half_width * half_height; ++i) { | 45 for (int i = 0; i < half_width * half_height; ++i) { |
58 u_plane[i] = static_cast<uint8>(start_value + i); | 46 u_plane[i] = static_cast<uint8>(start_value + i); |
59 } | 47 } |
60 | 48 |
61 // Set V. | 49 // Set V. |
62 for (int i = 0; i < half_width * half_height; ++i) { | 50 for (int i = 0; i < half_width * half_height; ++i) { |
63 v_plane[i] = static_cast<uint8>(start_value + i); | 51 v_plane[i] = static_cast<uint8>(start_value + i); |
64 } | 52 } |
65 } | 53 } |
66 | 54 |
67 void PopulateVideoFrame(I420VideoFrame* frame, int start_value) { | |
68 int half_width = (frame->width + 1) / 2; | |
69 int half_height = (frame->height + 1) / 2; | |
70 frame->y_plane.stride = frame->width; | |
71 frame->y_plane.length = frame->width * frame->height; | |
72 frame->y_plane.data = new uint8[frame->y_plane.length]; | |
73 | |
74 frame->u_plane.stride = half_width; | |
75 frame->u_plane.length = half_width * half_height; | |
76 frame->u_plane.data = new uint8[frame->u_plane.length]; | |
77 | |
78 frame->v_plane.stride = half_width; | |
79 frame->v_plane.length = half_width * half_height; | |
80 frame->v_plane.data = new uint8[frame->v_plane.length]; | |
81 | |
82 // Set Y. | |
83 for (int i = 0; i < frame->y_plane.length; ++i) { | |
84 frame->y_plane.data[i] = static_cast<uint8>(start_value + i); | |
85 } | |
86 | |
87 // Set U. | |
88 for (int i = 0; i < frame->u_plane.length; ++i) { | |
89 frame->u_plane.data[i] = static_cast<uint8>(start_value + i); | |
90 } | |
91 | |
92 // Set V. | |
93 for (int i = 0; i < frame->v_plane.length; ++i) { | |
94 frame->v_plane.data[i] = static_cast<uint8>(start_value + i); | |
95 } | |
96 } | |
97 | |
98 bool PopulateVideoFrameFromFile(VideoFrame* frame, FILE* video_file) { | 55 bool PopulateVideoFrameFromFile(VideoFrame* frame, FILE* video_file) { |
99 int width = frame->coded_size().width(); | 56 int width = frame->coded_size().width(); |
100 int height = frame->coded_size().height(); | 57 int height = frame->coded_size().height(); |
101 int half_width = (width + 1) / 2; | 58 int half_width = (width + 1) / 2; |
102 int half_height = (height + 1) / 2; | 59 int half_height = (height + 1) / 2; |
103 size_t frame_size = width * height + 2 * half_width * half_height; | 60 size_t frame_size = width * height + 2 * half_width * half_height; |
104 uint8* y_plane = frame->data(VideoFrame::kYPlane); | 61 uint8* y_plane = frame->data(VideoFrame::kYPlane); |
105 uint8* u_plane = frame->data(VideoFrame::kUPlane); | 62 uint8* u_plane = frame->data(VideoFrame::kUPlane); |
106 uint8* v_plane = frame->data(VideoFrame::kVPlane); | 63 uint8* v_plane = frame->data(VideoFrame::kVPlane); |
107 | 64 |
108 uint8* raw_data = new uint8[frame_size]; | 65 uint8* raw_data = new uint8[frame_size]; |
109 size_t count = fread(raw_data, 1, frame_size, video_file); | 66 size_t count = fread(raw_data, 1, frame_size, video_file); |
110 if (count != frame_size) return false; | 67 if (count != frame_size) return false; |
111 | 68 |
112 memcpy(y_plane, raw_data, width * height); | 69 memcpy(y_plane, raw_data, width * height); |
113 memcpy(u_plane, raw_data + width * height, half_width * half_height); | 70 memcpy(u_plane, raw_data + width * height, half_width * half_height); |
114 memcpy(v_plane, raw_data + width * height + | 71 memcpy(v_plane, raw_data + width * height + |
115 half_width * half_height, half_width * half_height); | 72 half_width * half_height, half_width * half_height); |
116 delete [] raw_data; | 73 delete [] raw_data; |
117 return true; | 74 return true; |
118 } | 75 } |
119 | 76 |
120 } // namespace cast | 77 } // namespace cast |
121 } // namespace media | 78 } // namespace media |
OLD | NEW |