vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / media-add-ons / demultiplexer / misc.cpp
blob037ba0b8c536e0ccb178a19229c14c7927edc05b
1 // misc.cpp
2 //
3 // Andrew Bachmann, 2002
4 //
5 // Some functions for general debugging and
6 // working around be media kit bugs.
8 #include "misc.h"
9 #include <stdio.h>
11 // -------------------------------------------------------- //
12 // lib functions
13 // -------------------------------------------------------- //
15 void print_multistream_format(media_multistream_format * format) {
16 fprintf(stderr,"[");
17 switch (format->format) {
18 case media_multistream_format::B_ANY: fprintf(stderr,"ANY"); break;
19 case media_multistream_format::B_VID: fprintf(stderr,"VID"); break;
20 case media_multistream_format::B_AVI: fprintf(stderr,"AVI"); break;
21 case media_multistream_format::B_MPEG1: fprintf(stderr,"MPEG1"); break;
22 case media_multistream_format::B_MPEG2: fprintf(stderr,"MPEG2"); break;
23 case media_multistream_format::B_QUICKTIME: fprintf(stderr,"QUICKTIME"); break;
24 default: fprintf(stderr,"????"); break;
26 fprintf(stderr," avg_bit_rate(%f) max_bit_rate(%f)",
27 format->avg_bit_rate,format->max_bit_rate);
28 fprintf(stderr," avg_chunk_size(%i) max_chunk_size(%i)",
29 format->avg_chunk_size,format->max_chunk_size);
32 void print_media_format(media_format * format) {
33 fprintf(stderr,"{");
34 switch (format->type) {
35 case B_MEDIA_NO_TYPE: fprintf(stderr,"NO_TYPE"); break;
36 case B_MEDIA_UNKNOWN_TYPE: fprintf(stderr,"UNKNOWN_TYPE"); break;
37 case B_MEDIA_RAW_AUDIO: fprintf(stderr,"RAW_AUDIO"); break;
38 case B_MEDIA_RAW_VIDEO: fprintf(stderr,"RAW_VIDEO"); break;
39 case B_MEDIA_VBL: fprintf(stderr,"VBL"); break;
40 case B_MEDIA_TIMECODE: fprintf(stderr,"TIMECODE"); break;
41 case B_MEDIA_MIDI: fprintf(stderr,"MIDI"); break;
42 case B_MEDIA_TEXT: fprintf(stderr,"TEXT"); break;
43 case B_MEDIA_HTML: fprintf(stderr,"HTML"); break;
44 case B_MEDIA_MULTISTREAM: fprintf(stderr,"MULTISTREAM"); break;
45 case B_MEDIA_PARAMETERS: fprintf(stderr,"PARAMETERS"); break;
46 case B_MEDIA_ENCODED_AUDIO: fprintf(stderr,"ENCODED_AUDIO"); break;
47 case B_MEDIA_ENCODED_VIDEO: fprintf(stderr,"ENCODED_VIDEO"); break;
48 default: fprintf(stderr,"????"); break;
50 fprintf(stderr,":");
51 switch (format->type) {
52 case B_MEDIA_RAW_AUDIO: fprintf(stderr,"RAW_AUDIO"); break;
53 case B_MEDIA_RAW_VIDEO: fprintf(stderr,"RAW_VIDEO"); break;
54 case B_MEDIA_MULTISTREAM: print_multistream_format(&format->u.multistream); break;
55 case B_MEDIA_ENCODED_AUDIO: fprintf(stderr,"ENCODED_AUDIO"); break;
56 case B_MEDIA_ENCODED_VIDEO: fprintf(stderr,"ENCODED_VIDEO"); break;
57 default: fprintf(stderr,"????"); break;
59 fprintf(stderr,"}");
62 bool multistream_format_is_acceptible(
63 const media_multistream_format & producer_format,
64 const media_multistream_format & consumer_format)
66 // first check the format, if necessary
67 if (consumer_format.format != media_multistream_format::B_ANY) {
68 if (consumer_format.format != producer_format.format) {
69 return false;
72 // then check the average bit rate
73 if (consumer_format.avg_bit_rate != media_multistream_format::wildcard.avg_bit_rate) {
74 if (consumer_format.avg_bit_rate != producer_format.avg_bit_rate) {
75 // do they have to match exactly? I don't know. assume yes.
76 return false;
79 // then check the maximum bit rate
80 if (consumer_format.max_bit_rate != media_multistream_format::wildcard.max_bit_rate) {
81 if (consumer_format.max_bit_rate != producer_format.max_bit_rate) {
82 // do they have to match exactly? I don't know. assume yes.
83 return false;
86 // then check the average chunk size
87 if (consumer_format.avg_chunk_size != media_multistream_format::wildcard.avg_chunk_size) {
88 if (consumer_format.avg_chunk_size != producer_format.avg_chunk_size) {
89 // do they have to match exactly? I don't know. assume yes.
90 return false;
93 // then check the maximum bit rate
94 if (consumer_format.max_chunk_size != media_multistream_format::wildcard.max_chunk_size) {
95 if (consumer_format.max_chunk_size != producer_format.max_chunk_size) {
96 // do they have to match exactly? I don't know. assume yes.
97 return false;
100 // should also check format specific fields, and others?
101 return true;
104 bool format_is_acceptible(
105 const media_format & producer_format,
106 const media_format & consumer_format)
108 // first check the type, if necessary
109 if (consumer_format.type != B_MEDIA_UNKNOWN_TYPE) {
110 if (consumer_format.type != producer_format.type) {
111 return false;
113 switch (consumer_format.type) {
114 case B_MEDIA_MULTISTREAM:
115 if (!multistream_format_is_acceptible(producer_format.u.multistream,
116 consumer_format.u.multistream)) {
117 return false;
119 break;
120 default:
121 fprintf(stderr,"format_is_acceptible : unimplemented type.\n");
122 return format_is_compatible(producer_format,consumer_format);
123 break;
126 // should also check non-type fields?
127 return true;