xref: /haiku/src/add-ons/media/media-add-ons/dvb/MediaFormat.cpp (revision b0b3cab0fd5422285eea4458b96e63fd28911eb4)
1b2859abeSMarcus Overhagen /*
2b2859abeSMarcus Overhagen  * Copyright (c) 2004-2007 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 
40*b0b3cab0SMarcus Overhagen void
41*b0b3cab0SMarcus Overhagen PrintFormat(const media_output &output)
42b2859abeSMarcus Overhagen {
43*b0b3cab0SMarcus Overhagen 	PrintFormat(output.format);
44*b0b3cab0SMarcus Overhagen }
45b2859abeSMarcus Overhagen 
46b2859abeSMarcus Overhagen 
47*b0b3cab0SMarcus Overhagen static status_t
48*b0b3cab0SMarcus Overhagen GetHeaderFormatAc3Audio(media_format *out_format, const uint8 *header, size_t size)
49*b0b3cab0SMarcus Overhagen {
50*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormatAc3Audio\n");
51b2859abeSMarcus Overhagen 
52*b0b3cab0SMarcus Overhagen 	status_t status;
53b2859abeSMarcus Overhagen 	media_format_description desc;
54*b0b3cab0SMarcus Overhagen 	desc.family = B_WAV_FORMAT_FAMILY;
55*b0b3cab0SMarcus Overhagen 	desc.u.wav.codec = 0x2000;
56b2859abeSMarcus Overhagen 
57b2859abeSMarcus Overhagen 	BMediaFormats formats;
58*b0b3cab0SMarcus Overhagen 	status = formats.InitCheck();
59*b0b3cab0SMarcus Overhagen 	if (status)
60*b0b3cab0SMarcus Overhagen 		return status;
61b2859abeSMarcus Overhagen 
62*b0b3cab0SMarcus Overhagen 	return formats.GetFormatFor(desc, out_format);
63*b0b3cab0SMarcus Overhagen }
64b2859abeSMarcus Overhagen 
65*b0b3cab0SMarcus Overhagen 
66*b0b3cab0SMarcus Overhagen static status_t
67*b0b3cab0SMarcus Overhagen GetHeaderFormatDtsAudio(media_format *out_format, const uint8 *header, size_t size)
68*b0b3cab0SMarcus Overhagen {
69*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormatDtsAudio: unsupported\n");
70*b0b3cab0SMarcus Overhagen 	return B_ERROR;
71*b0b3cab0SMarcus Overhagen }
72*b0b3cab0SMarcus Overhagen 
73*b0b3cab0SMarcus Overhagen 
74*b0b3cab0SMarcus Overhagen static status_t
75*b0b3cab0SMarcus Overhagen GetHeaderFormatLpcmAudio(media_format *out_format, const uint8 *header, size_t size)
76*b0b3cab0SMarcus Overhagen {
77*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormatLpcmAudio: unsupported\n");
78*b0b3cab0SMarcus Overhagen 	return B_ERROR;
79*b0b3cab0SMarcus Overhagen }
80*b0b3cab0SMarcus Overhagen 
81*b0b3cab0SMarcus Overhagen 
82*b0b3cab0SMarcus Overhagen static status_t
83*b0b3cab0SMarcus Overhagen GetHeaderFormatPrivateStream(media_format *out_format, const uint8 *header, size_t size)
84*b0b3cab0SMarcus Overhagen {
85*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormatPrivateStream: unsupported, assuming AC3\n");
86*b0b3cab0SMarcus Overhagen 	return GetHeaderFormatAc3Audio(out_format, header, size);
87*b0b3cab0SMarcus Overhagen }
88*b0b3cab0SMarcus Overhagen 
89*b0b3cab0SMarcus Overhagen 
90*b0b3cab0SMarcus Overhagen static status_t
91*b0b3cab0SMarcus Overhagen GetHeaderFormatMpegAudio(media_format *out_format, const uint8 *header, size_t size)
92*b0b3cab0SMarcus Overhagen {
93*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormatMpegAudio\n");
94*b0b3cab0SMarcus Overhagen 
95*b0b3cab0SMarcus Overhagen 	status_t status;
96*b0b3cab0SMarcus Overhagen 	media_format_description desc;
97*b0b3cab0SMarcus Overhagen 	desc.family = B_MPEG_FORMAT_FAMILY;
98*b0b3cab0SMarcus Overhagen 	desc.u.mpeg.id = B_MPEG_2_AUDIO_LAYER_2;
99*b0b3cab0SMarcus Overhagen 
100*b0b3cab0SMarcus Overhagen 	BMediaFormats formats;
101*b0b3cab0SMarcus Overhagen 	status = formats.InitCheck();
102*b0b3cab0SMarcus Overhagen 	if (status)
103*b0b3cab0SMarcus Overhagen 		return status;
104*b0b3cab0SMarcus Overhagen 
105*b0b3cab0SMarcus Overhagen 	status = formats.GetFormatFor(desc, out_format);
106*b0b3cab0SMarcus Overhagen 	if (status)
107*b0b3cab0SMarcus Overhagen 		return status;
108b2859abeSMarcus Overhagen 
109b2859abeSMarcus Overhagen 	out_format->u.encoded_audio.output.frame_rate = 48000;
110b2859abeSMarcus Overhagen 	out_format->u.encoded_audio.output.channel_count = 2;
111b2859abeSMarcus Overhagen 	out_format->u.encoded_audio.output.buffer_size = 1024;
112b2859abeSMarcus Overhagen 	return B_OK;
113b2859abeSMarcus Overhagen }
114*b0b3cab0SMarcus Overhagen 
115*b0b3cab0SMarcus Overhagen 
116*b0b3cab0SMarcus Overhagen static status_t
117*b0b3cab0SMarcus Overhagen GetHeaderFormatMpegVideo(media_format *out_format, const uint8 *header, size_t size)
118*b0b3cab0SMarcus Overhagen {
119*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormatMpegVideo\n");
120*b0b3cab0SMarcus Overhagen 
121*b0b3cab0SMarcus Overhagen 	status_t status;
122*b0b3cab0SMarcus Overhagen 	media_format_description desc;
123*b0b3cab0SMarcus Overhagen 	desc.family = B_MPEG_FORMAT_FAMILY;
124*b0b3cab0SMarcus Overhagen 	desc.u.mpeg.id = B_MPEG_2_VIDEO;
125*b0b3cab0SMarcus Overhagen 
126*b0b3cab0SMarcus Overhagen 	BMediaFormats formats;
127*b0b3cab0SMarcus Overhagen 	status = formats.InitCheck();
128*b0b3cab0SMarcus Overhagen 	if (status)
129*b0b3cab0SMarcus Overhagen 		return status;
130*b0b3cab0SMarcus Overhagen 
131*b0b3cab0SMarcus Overhagen 	return formats.GetFormatFor(desc, out_format);
132*b0b3cab0SMarcus Overhagen }
133*b0b3cab0SMarcus Overhagen 
134*b0b3cab0SMarcus Overhagen 
135*b0b3cab0SMarcus Overhagen status_t
136*b0b3cab0SMarcus Overhagen GetHeaderFormat(media_format *out_format, const void *header, size_t size, int stream_id)
137*b0b3cab0SMarcus Overhagen {
138*b0b3cab0SMarcus Overhagen 	const uint8 *h = (const uint8 *)header;
139*b0b3cab0SMarcus Overhagen 	status_t status;
140*b0b3cab0SMarcus Overhagen 
141*b0b3cab0SMarcus Overhagen 	printf("GetHeaderFormat: stream_id %02x\n", stream_id);
142*b0b3cab0SMarcus Overhagen 	printf("inner frame header: "
143*b0b3cab0SMarcus Overhagen 		   "%02x %02x %02x %02x %02x %02x %02x %02x "
144*b0b3cab0SMarcus Overhagen 		   "%02x %02x %02x %02x %02x %02x %02x %02x\n",
145*b0b3cab0SMarcus Overhagen 		   h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
146*b0b3cab0SMarcus Overhagen 		   h[8], h[9], h[10], h[11], h[12], h[13], h[14], h[15]);
147*b0b3cab0SMarcus Overhagen 
148*b0b3cab0SMarcus Overhagen 	if (stream_id >= 0x80 && stream_id <= 0x87)
149*b0b3cab0SMarcus Overhagen 		status = GetHeaderFormatAc3Audio(out_format, h, size);
150*b0b3cab0SMarcus Overhagen 	else if (stream_id >= 0x88 && stream_id <= 0x8F)
151*b0b3cab0SMarcus Overhagen 		status = GetHeaderFormatDtsAudio(out_format, h, size);
152*b0b3cab0SMarcus Overhagen 	else if (stream_id >= 0xA0 && stream_id <= 0xA7)
153*b0b3cab0SMarcus Overhagen 		status = GetHeaderFormatLpcmAudio(out_format, h, size);
154*b0b3cab0SMarcus Overhagen 	else if (stream_id == 0xBD)
155*b0b3cab0SMarcus Overhagen 		status = GetHeaderFormatPrivateStream(out_format, h, size);
156*b0b3cab0SMarcus Overhagen 	else if (stream_id >= 0xC0 && stream_id <= 0xDF)
157*b0b3cab0SMarcus Overhagen 		status = GetHeaderFormatMpegAudio(out_format, h, size);
158*b0b3cab0SMarcus Overhagen 	else if (stream_id >= 0xE0 && stream_id <= 0xEF)
159*b0b3cab0SMarcus Overhagen 		status = GetHeaderFormatMpegVideo(out_format, h, size);
160*b0b3cab0SMarcus Overhagen 	else {
161*b0b3cab0SMarcus Overhagen 		printf("GetHeaderFormat: don't know what this stream_id means\n");
162*b0b3cab0SMarcus Overhagen 		status = B_ERROR;
163*b0b3cab0SMarcus Overhagen 	}
164*b0b3cab0SMarcus Overhagen 
165*b0b3cab0SMarcus Overhagen 	if (status != B_OK) {
166*b0b3cab0SMarcus Overhagen 		printf("GetHeaderFormat: failed!\n");
167*b0b3cab0SMarcus Overhagen 	} else {
168*b0b3cab0SMarcus Overhagen 		printf("GetHeaderFormat: out_format ");
169*b0b3cab0SMarcus Overhagen 		PrintFormat(*out_format);
170*b0b3cab0SMarcus Overhagen 	}
171*b0b3cab0SMarcus Overhagen 	return status;
172*b0b3cab0SMarcus Overhagen }
173*b0b3cab0SMarcus Overhagen 
174