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