vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / matroska_util.cpp
blob4fe2ff4779897a3dd24d9d17208634d0af8fe759
1 /*
2 * Copyright (c) 2004, Marcus Overhagen
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <string.h>
26 #include <stdio.h>
27 #include "matroska_util.h"
30 bigtime_t
31 get_duration_in_us(uint64 duration)
33 if (duration > 0)
34 return duration / 1000 + ((duration % 1000) ? 1 : 0);
35 else
36 return 12 * 60 * 60 * 1000000LL;
40 float
41 get_frame_rate(uint64 default_duration)
43 if (default_duration > 1)
44 return 1000000000.0 / default_duration;
45 else
46 return 29.97;
50 uint64
51 get_frame_count_by_frame_rate(uint64 duration, float frame_rate)
53 return uint64((double(duration) * double(frame_rate)) / 1000000000.0);
57 uint64
58 get_frame_count_by_default_duration(uint64 duration, uint64 default_duration)
60 if (duration > 0 && default_duration > 1)
61 return (duration / default_duration) + ((duration % default_duration) ? 1 : 0);
62 if (duration > 0)
63 return (duration * 2997) / 100000000000ull;
64 return 12 * 60 * 60 * 2997 / 100;
68 void
69 get_pixel_aspect_ratio(uint16 *width_aspect_ratio, uint16 *height_aspect_ratio,
70 unsigned int pixel_width, unsigned int pixel_height,
71 unsigned int display_width, unsigned int display_height)
73 printf("get_pixel_aspect_ratio: pixel_width %u, pixel_height %u, display_width %u, display_height %u\n",
74 pixel_width, pixel_height, display_width, display_height);
76 if (pixel_width == display_width && pixel_height == display_height) {
77 *width_aspect_ratio = 1;
78 *height_aspect_ratio = 1;
79 return;
82 double w_aspect = display_width / (double)pixel_width;
83 double h_aspect = display_height / (double)pixel_height;
84 w_aspect /= h_aspect;
85 h_aspect = 1;
87 printf("w_aspect %.6f, h_aspect %.6f\n", w_aspect, h_aspect);
89 *width_aspect_ratio = uint16(w_aspect * 10000 + 0.5);
90 *height_aspect_ratio = 10000;
92 printf("w_aspect %.6f, h_aspect %.6f, ratio %u:%u\n", w_aspect, h_aspect, *width_aspect_ratio, *height_aspect_ratio);