xref: /haiku/src/add-ons/media/media-add-ons/dvb/MediaFormat.cpp (revision 93a78ecaa45114d68952d08c4778f073515102f2)
1 /*
2  * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify,
8  * merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <media/MediaFormats.h>
26 #include <media/MediaNode.h>
27 #include <stdio.h>
28 #include "MediaFormat.h"
29 
30 
31 void
32 PrintFormat(const media_format &format)
33 {
34 	char s[200];
35 	string_for_format(format, s, sizeof(s));
36 	printf("%s\n", s);
37 }
38 
39 
40 void
41 PrintFormat(const media_output &output)
42 {
43 	PrintFormat(output.format);
44 }
45 
46 
47 static status_t
48 GetHeaderFormatAc3Audio(media_format *out_format, const uint8 *header, size_t size)
49 {
50 	printf("GetHeaderFormatAc3Audio\n");
51 
52 	status_t status;
53 	media_format_description desc;
54 	desc.family = B_WAV_FORMAT_FAMILY;
55 	desc.u.wav.codec = 0x2000;
56 
57 	BMediaFormats formats;
58 	status = formats.InitCheck();
59 	if (status)
60 		return status;
61 
62 	return formats.GetFormatFor(desc, out_format);
63 }
64 
65 
66 static status_t
67 GetHeaderFormatDtsAudio(media_format *out_format, const uint8 *header, size_t size)
68 {
69 	printf("GetHeaderFormatDtsAudio: unsupported\n");
70 	return B_ERROR;
71 }
72 
73 
74 static status_t
75 GetHeaderFormatLpcmAudio(media_format *out_format, const uint8 *header, size_t size)
76 {
77 	printf("GetHeaderFormatLpcmAudio: unsupported\n");
78 	return B_ERROR;
79 }
80 
81 
82 static status_t
83 GetHeaderFormatPrivateStream(media_format *out_format, const uint8 *header, size_t size)
84 {
85 	printf("GetHeaderFormatPrivateStream: unsupported, assuming AC3\n");
86 	return GetHeaderFormatAc3Audio(out_format, header, size);
87 }
88 
89 
90 static status_t
91 GetHeaderFormatMpegAudio(media_format *out_format, const uint8 *header, size_t size)
92 {
93 	printf("GetHeaderFormatMpegAudio\n");
94 
95 	status_t status;
96 	media_format_description desc;
97 	desc.family = B_MPEG_FORMAT_FAMILY;
98 	desc.u.mpeg.id = B_MPEG_2_AUDIO_LAYER_2;
99 
100 	BMediaFormats formats;
101 	status = formats.InitCheck();
102 	if (status)
103 		return status;
104 
105 	status = formats.GetFormatFor(desc, out_format);
106 	if (status)
107 		return status;
108 
109 	out_format->u.encoded_audio.output.frame_rate = 48000;
110 	out_format->u.encoded_audio.output.channel_count = 2;
111 	out_format->u.encoded_audio.output.buffer_size = 1024;
112 	return B_OK;
113 }
114 
115 
116 static status_t
117 GetHeaderFormatMpegVideo(media_format *out_format, const uint8 *header, size_t size)
118 {
119 	printf("GetHeaderFormatMpegVideo\n");
120 
121 	status_t status;
122 	media_format_description desc;
123 	desc.family = B_MPEG_FORMAT_FAMILY;
124 	desc.u.mpeg.id = B_MPEG_2_VIDEO;
125 
126 	BMediaFormats formats;
127 	status = formats.InitCheck();
128 	if (status)
129 		return status;
130 
131 	return formats.GetFormatFor(desc, out_format);
132 }
133 
134 
135 status_t
136 GetHeaderFormat(media_format *out_format, const void *header, size_t size, int stream_id)
137 {
138 	const uint8 *h = (const uint8 *)header;
139 	status_t status;
140 
141 	printf("GetHeaderFormat: stream_id %02x\n", stream_id);
142 	printf("inner frame header: "
143 		   "%02x %02x %02x %02x %02x %02x %02x %02x "
144 		   "%02x %02x %02x %02x %02x %02x %02x %02x\n",
145 		   h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
146 		   h[8], h[9], h[10], h[11], h[12], h[13], h[14], h[15]);
147 
148 	if (stream_id >= 0x80 && stream_id <= 0x87)
149 		status = GetHeaderFormatAc3Audio(out_format, h, size);
150 	else if (stream_id >= 0x88 && stream_id <= 0x8F)
151 		status = GetHeaderFormatDtsAudio(out_format, h, size);
152 	else if (stream_id >= 0xA0 && stream_id <= 0xA7)
153 		status = GetHeaderFormatLpcmAudio(out_format, h, size);
154 	else if (stream_id == 0xBD)
155 		status = GetHeaderFormatPrivateStream(out_format, h, size);
156 	else if (stream_id >= 0xC0 && stream_id <= 0xDF)
157 		status = GetHeaderFormatMpegAudio(out_format, h, size);
158 	else if (stream_id >= 0xE0 && stream_id <= 0xEF)
159 		status = GetHeaderFormatMpegVideo(out_format, h, size);
160 	else {
161 		printf("GetHeaderFormat: don't know what this stream_id means\n");
162 		status = B_ERROR;
163 	}
164 
165 	if (status != B_OK) {
166 		printf("GetHeaderFormat: failed!\n");
167 	} else {
168 		printf("GetHeaderFormat: out_format ");
169 		PrintFormat(*out_format);
170 	}
171 	return status;
172 }
173 
174