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