Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: media/base/video_frame.cc

Issue 839523002: V4L2VDA: Generalize EGLImage import and query driver for output formats. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/video_frame.h ('k') | media/blink/video_frame_compositor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/base/video_frame.h" 5 #include "media/base/video_frame.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 case VideoFrame::I420: 60 case VideoFrame::I420:
61 case VideoFrame::YV12A: 61 case VideoFrame::YV12A:
62 case VideoFrame::NV12: 62 case VideoFrame::NV12:
63 return gfx::Size(2, 2); 63 return gfx::Size(2, 2);
64 64
65 case VideoFrame::UNKNOWN: 65 case VideoFrame::UNKNOWN:
66 #if defined(VIDEO_HOLE) 66 #if defined(VIDEO_HOLE)
67 case VideoFrame::HOLE: 67 case VideoFrame::HOLE:
68 #endif // defined(VIDEO_HOLE) 68 #endif // defined(VIDEO_HOLE)
69 case VideoFrame::NATIVE_TEXTURE: 69 case VideoFrame::NATIVE_TEXTURE:
70 case VideoFrame::ARGB:
70 break; 71 break;
71 } 72 }
72 } 73 }
73 NOTREACHED(); 74 NOTREACHED();
74 return gfx::Size(); 75 return gfx::Size();
75 } 76 }
76 77
77 // Return the alignment for the whole frame, calculated as the max of the 78 // Return the alignment for the whole frame, calculated as the max of the
78 // alignment for each individual plane. 79 // alignment for each individual plane.
79 static gfx::Size CommonAlignment(VideoFrame::Format format) { 80 static gfx::Size CommonAlignment(VideoFrame::Format format) {
80 int max_sample_width = 0; 81 int max_sample_width = 0;
81 int max_sample_height = 0; 82 int max_sample_height = 0;
82 for (size_t plane = 0; plane < VideoFrame::NumPlanes(format); ++plane) { 83 for (size_t plane = 0; plane < VideoFrame::NumPlanes(format); ++plane) {
83 const gfx::Size sample_size = SampleSize(format, plane); 84 const gfx::Size sample_size = SampleSize(format, plane);
84 max_sample_width = std::max(max_sample_width, sample_size.width()); 85 max_sample_width = std::max(max_sample_width, sample_size.width());
85 max_sample_height = std::max(max_sample_height, sample_size.height()); 86 max_sample_height = std::max(max_sample_height, sample_size.height());
86 } 87 }
87 return gfx::Size(max_sample_width, max_sample_height); 88 return gfx::Size(max_sample_width, max_sample_height);
88 } 89 }
89 90
90 // Returns the number of bytes per element for given |plane| and |format|. E.g. 91 // Returns the number of bytes per element for given |plane| and |format|. E.g.
91 // 2 for the UV plane in NV12. 92 // 2 for the UV plane in NV12.
92 static int BytesPerElement(VideoFrame::Format format, size_t plane) { 93 static int BytesPerElement(VideoFrame::Format format, size_t plane) {
93 DCHECK(VideoFrame::IsValidPlane(plane, format)); 94 DCHECK(VideoFrame::IsValidPlane(plane, format));
94 return (format == VideoFrame::NV12 && plane == VideoFrame::kUVPlane) ? 2 : 1; 95 if (format == VideoFrame::ARGB)
96 return 4;
97
98 if (format == VideoFrame::NV12 && plane == VideoFrame::kUVPlane)
99 return 2;
100
101 return 1;
95 } 102 }
96 103
97 // Rounds up |coded_size| if necessary for |format|. 104 // Rounds up |coded_size| if necessary for |format|.
98 static gfx::Size AdjustCodedSize(VideoFrame::Format format, 105 static gfx::Size AdjustCodedSize(VideoFrame::Format format,
99 const gfx::Size& coded_size) { 106 const gfx::Size& coded_size) {
100 const gfx::Size alignment = CommonAlignment(format); 107 const gfx::Size alignment = CommonAlignment(format);
101 return gfx::Size(RoundUp(coded_size.width(), alignment.width()), 108 return gfx::Size(RoundUp(coded_size.width(), alignment.width()),
102 RoundUp(coded_size.height(), alignment.height())); 109 RoundUp(coded_size.height(), alignment.height()));
103 } 110 }
104 111
105 // static 112 // static
106 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( 113 scoped_refptr<VideoFrame> VideoFrame::CreateFrame(
107 VideoFrame::Format format, 114 VideoFrame::Format format,
108 const gfx::Size& coded_size, 115 const gfx::Size& coded_size,
109 const gfx::Rect& visible_rect, 116 const gfx::Rect& visible_rect,
110 const gfx::Size& natural_size, 117 const gfx::Size& natural_size,
111 base::TimeDelta timestamp) { 118 base::TimeDelta timestamp) {
112 DCHECK(format != VideoFrame::UNKNOWN && 119 switch (format) {
113 format != VideoFrame::NV12 && 120 case VideoFrame::YV12:
114 format != VideoFrame::NATIVE_TEXTURE); 121 case VideoFrame::YV16:
122 case VideoFrame::I420:
123 case VideoFrame::YV12A:
124 case VideoFrame::YV12J:
125 case VideoFrame::YV24:
126 break;
127
128 case VideoFrame::UNKNOWN:
129 case VideoFrame::NV12:
130 case VideoFrame::NATIVE_TEXTURE:
115 #if defined(VIDEO_HOLE) 131 #if defined(VIDEO_HOLE)
116 DCHECK(format != VideoFrame::HOLE); 132 case VideoFrame::HOLE:
117 #endif // defined(VIDEO_HOLE) 133 #endif // defined(VIDEO_HOLE)
134 case VideoFrame::ARGB:
135 NOTIMPLEMENTED();
136 return nullptr;
137 }
118 138
119 // Since we're creating a new YUV frame (and allocating memory for it 139 // Since we're creating a new YUV frame (and allocating memory for it
120 // ourselves), we can pad the requested |coded_size| if necessary if the 140 // ourselves), we can pad the requested |coded_size| if necessary if the
121 // request does not line up on sample boundaries. 141 // request does not line up on sample boundaries.
122 const gfx::Size new_coded_size = AdjustCodedSize(format, coded_size); 142 const gfx::Size new_coded_size = AdjustCodedSize(format, coded_size);
123 DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size)); 143 DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size));
124 144
125 scoped_refptr<VideoFrame> frame( 145 scoped_refptr<VideoFrame> frame(
126 new VideoFrame(format, 146 new VideoFrame(format,
127 new_coded_size, 147 new_coded_size,
(...skipping 24 matching lines...) Expand all
152 return "HOLE"; 172 return "HOLE";
153 #endif // defined(VIDEO_HOLE) 173 #endif // defined(VIDEO_HOLE)
154 case VideoFrame::YV12A: 174 case VideoFrame::YV12A:
155 return "YV12A"; 175 return "YV12A";
156 case VideoFrame::YV12J: 176 case VideoFrame::YV12J:
157 return "YV12J"; 177 return "YV12J";
158 case VideoFrame::NV12: 178 case VideoFrame::NV12:
159 return "NV12"; 179 return "NV12";
160 case VideoFrame::YV24: 180 case VideoFrame::YV24:
161 return "YV24"; 181 return "YV24";
182 case VideoFrame::ARGB:
183 return "ARGB";
162 } 184 }
163 NOTREACHED() << "Invalid videoframe format provided: " << format; 185 NOTREACHED() << "Invalid videoframe format provided: " << format;
164 return ""; 186 return "";
165 } 187 }
166 188
167 // static 189 // static
168 bool VideoFrame::IsValidConfig(VideoFrame::Format format, 190 bool VideoFrame::IsValidConfig(VideoFrame::Format format,
169 const gfx::Size& coded_size, 191 const gfx::Size& coded_size,
170 const gfx::Rect& visible_rect, 192 const gfx::Rect& visible_rect,
171 const gfx::Size& natural_size) { 193 const gfx::Size& natural_size) {
(...skipping 23 matching lines...) Expand all
195 #endif // defined(VIDEO_HOLE) 217 #endif // defined(VIDEO_HOLE)
196 return true; 218 return true;
197 219
198 case VideoFrame::YV24: 220 case VideoFrame::YV24:
199 case VideoFrame::YV12: 221 case VideoFrame::YV12:
200 case VideoFrame::YV12J: 222 case VideoFrame::YV12J:
201 case VideoFrame::I420: 223 case VideoFrame::I420:
202 case VideoFrame::YV12A: 224 case VideoFrame::YV12A:
203 case VideoFrame::NV12: 225 case VideoFrame::NV12:
204 case VideoFrame::YV16: 226 case VideoFrame::YV16:
227 case VideoFrame::ARGB:
205 // Check that software-allocated buffer formats are aligned correctly and 228 // Check that software-allocated buffer formats are aligned correctly and
206 // not empty. 229 // not empty.
207 const gfx::Size alignment = CommonAlignment(format); 230 const gfx::Size alignment = CommonAlignment(format);
208 return RoundUp(visible_rect.right(), alignment.width()) <= 231 return RoundUp(visible_rect.right(), alignment.width()) <=
209 static_cast<size_t>(coded_size.width()) && 232 static_cast<size_t>(coded_size.width()) &&
210 RoundUp(visible_rect.bottom(), alignment.height()) <= 233 RoundUp(visible_rect.bottom(), alignment.height()) <=
211 static_cast<size_t>(coded_size.height()) && 234 static_cast<size_t>(coded_size.height()) &&
212 !coded_size.IsEmpty() && !visible_rect.IsEmpty() && 235 !coded_size.IsEmpty() && !visible_rect.IsEmpty() &&
213 !natural_size.IsEmpty(); 236 !natural_size.IsEmpty();
214 } 237 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 Format format, 320 Format format,
298 const gfx::Size& coded_size, 321 const gfx::Size& coded_size,
299 const gfx::Rect& visible_rect, 322 const gfx::Rect& visible_rect,
300 const gfx::Size& natural_size, 323 const gfx::Size& natural_size,
301 const std::vector<int> dmabuf_fds, 324 const std::vector<int> dmabuf_fds,
302 base::TimeDelta timestamp, 325 base::TimeDelta timestamp,
303 const base::Closure& no_longer_needed_cb) { 326 const base::Closure& no_longer_needed_cb) {
304 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) 327 if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
305 return NULL; 328 return NULL;
306 329
330 // TODO(posciak): This is not exactly correct, it's possible for one
331 // buffer to contain more than one plane.
307 if (dmabuf_fds.size() != NumPlanes(format)) { 332 if (dmabuf_fds.size() != NumPlanes(format)) {
308 LOG(FATAL) << "Not enough dmabuf fds provided!"; 333 LOG(FATAL) << "Not enough dmabuf fds provided!";
309 return NULL; 334 return NULL;
310 } 335 }
311 336
312 scoped_refptr<VideoFrame> frame( 337 scoped_refptr<VideoFrame> frame(
313 new VideoFrame(format, 338 new VideoFrame(format,
314 coded_size, 339 coded_size,
315 visible_rect, 340 visible_rect,
316 natural_size, 341 natural_size,
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 #endif // defined(VIDEO_HOLE) 540 #endif // defined(VIDEO_HOLE)
516 541
517 // static 542 // static
518 size_t VideoFrame::NumPlanes(Format format) { 543 size_t VideoFrame::NumPlanes(Format format) {
519 switch (format) { 544 switch (format) {
520 case VideoFrame::NATIVE_TEXTURE: 545 case VideoFrame::NATIVE_TEXTURE:
521 #if defined(VIDEO_HOLE) 546 #if defined(VIDEO_HOLE)
522 case VideoFrame::HOLE: 547 case VideoFrame::HOLE:
523 #endif // defined(VIDEO_HOLE) 548 #endif // defined(VIDEO_HOLE)
524 return 0; 549 return 0;
550 case VideoFrame::ARGB:
551 return 1;
525 case VideoFrame::NV12: 552 case VideoFrame::NV12:
526 return 2; 553 return 2;
527 case VideoFrame::YV12: 554 case VideoFrame::YV12:
528 case VideoFrame::YV16: 555 case VideoFrame::YV16:
529 case VideoFrame::I420: 556 case VideoFrame::I420:
530 case VideoFrame::YV12J: 557 case VideoFrame::YV12J:
531 case VideoFrame::YV24: 558 case VideoFrame::YV24:
532 return 3; 559 return 3;
533 case VideoFrame::YV12A: 560 case VideoFrame::YV12A:
534 return 4; 561 return 4;
(...skipping 12 matching lines...) Expand all
547 total += PlaneAllocationSize(format, i, coded_size); 574 total += PlaneAllocationSize(format, i, coded_size);
548 return total; 575 return total;
549 } 576 }
550 577
551 // static 578 // static
552 gfx::Size VideoFrame::PlaneSize(Format format, 579 gfx::Size VideoFrame::PlaneSize(Format format,
553 size_t plane, 580 size_t plane,
554 const gfx::Size& coded_size) { 581 const gfx::Size& coded_size) {
555 DCHECK(IsValidPlane(plane, format)); 582 DCHECK(IsValidPlane(plane, format));
556 583
557 // Align to multiple-of-two size overall. This ensures that non-subsampled 584 int width = coded_size.width();
558 // planes can be addressed by pixel with the same scaling as the subsampled 585 int height = coded_size.height();
559 // planes. 586 if (format != VideoFrame::ARGB) {
560 const int width = RoundUp(coded_size.width(), 2); 587 // Align to multiple-of-two size overall. This ensures that non-subsampled
561 const int height = RoundUp(coded_size.height(), 2); 588 // planes can be addressed by pixel with the same scaling as the subsampled
589 // planes.
590 width = RoundUp(width, 2);
591 height = RoundUp(height, 2);
592 }
562 593
563 const gfx::Size subsample = SampleSize(format, plane); 594 const gfx::Size subsample = SampleSize(format, plane);
564 DCHECK(width % subsample.width() == 0); 595 DCHECK(width % subsample.width() == 0);
565 DCHECK(height % subsample.height() == 0); 596 DCHECK(height % subsample.height() == 0);
566 return gfx::Size(BytesPerElement(format, plane) * width / subsample.width(), 597 return gfx::Size(BytesPerElement(format, plane) * width / subsample.width(),
567 height / subsample.height()); 598 height / subsample.height());
568 } 599 }
569 600
570 size_t VideoFrame::PlaneAllocationSize(Format format, 601 size_t VideoFrame::PlaneAllocationSize(Format format,
571 size_t plane, 602 size_t plane,
572 const gfx::Size& coded_size) { 603 const gfx::Size& coded_size) {
573 // VideoFrame formats are (so far) all YUV and 1 byte per sample.
574 return PlaneSize(format, plane, coded_size).GetArea(); 604 return PlaneSize(format, plane, coded_size).GetArea();
575 } 605 }
576 606
577 // static 607 // static
578 int VideoFrame::PlaneHorizontalBitsPerPixel(Format format, size_t plane) { 608 int VideoFrame::PlaneHorizontalBitsPerPixel(Format format, size_t plane) {
579 DCHECK(IsValidPlane(plane, format)); 609 DCHECK(IsValidPlane(plane, format));
580 const int bits_per_element = 8 * BytesPerElement(format, plane); 610 const int bits_per_element = 8 * BytesPerElement(format, plane);
581 const int pixels_per_element = SampleSize(format, plane).width(); 611 const int horiz_pixels_per_element = SampleSize(format, plane).width();
582 DCHECK(bits_per_element % pixels_per_element == 0); 612 DCHECK_EQ(bits_per_element % horiz_pixels_per_element, 0);
583 return bits_per_element / pixels_per_element; 613 return bits_per_element / horiz_pixels_per_element;
584 } 614 }
585 615
586 // static 616 // static
587 int VideoFrame::PlaneBitsPerPixel(Format format, size_t plane) { 617 int VideoFrame::PlaneBitsPerPixel(Format format, size_t plane) {
588 DCHECK(IsValidPlane(plane, format)); 618 DCHECK(IsValidPlane(plane, format));
589 return PlaneHorizontalBitsPerPixel(format, plane) / 619 return PlaneHorizontalBitsPerPixel(format, plane) /
590 SampleSize(format, plane).height(); 620 SampleSize(format, plane).height();
591 } 621 }
592 622
593 // Release data allocated by AllocateYUV(). 623 // Release data allocated by AllocateYUV().
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) { 808 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) {
779 for (int row = 0; row < rows(plane); ++row) { 809 for (int row = 0; row < rows(plane); ++row) {
780 base::MD5Update(context, base::StringPiece( 810 base::MD5Update(context, base::StringPiece(
781 reinterpret_cast<char*>(data(plane) + stride(plane) * row), 811 reinterpret_cast<char*>(data(plane) + stride(plane) * row),
782 row_bytes(plane))); 812 row_bytes(plane)));
783 } 813 }
784 } 814 }
785 } 815 }
786 816
787 } // namespace media 817 } // namespace media
OLDNEW
« no previous file with comments | « media/base/video_frame.h ('k') | media/blink/video_frame_compositor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698