xref: /haiku/src/add-ons/media/media-add-ons/dvb/MediaFormat.cpp (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 /*
2  * Copyright (c) 2004-2010 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 #define UINT64_C(c) (c ## ULL)
31 extern "C" {
32   #include "avcodec.h"
33 }
34 
35 void
36 PrintFormat(const media_format &format)
37 {
38 	char s[200];
39 	string_for_format(format, s, sizeof(s));
40 	printf("%s\n", s);
41 }
42 
43 
44 void
45 PrintFormat(const media_output &output)
46 {
47 	PrintFormat(output.format);
48 }
49 
50 
51 static status_t
52 GetHeaderFormatAc3Audio(media_format *out_format, const uint8 *header, size_t size)
53 {
54 	printf("GetHeaderFormatAc3Audio\n");
55 
56 	status_t status;
57 	media_format_description desc;
58 //	desc.family = B_WAV_FORMAT_FAMILY;
59 //	desc.u.wav.codec = 0x2000;
60 	desc.family = B_MISC_FORMAT_FAMILY;
61 	desc.u.misc.file_format = 'ffmp';
62 	desc.u.misc.codec = CODEC_ID_AC3;
63 
64 	BMediaFormats formats;
65 	status = formats.InitCheck();
66 	if (status) {
67 		printf("formats.InitCheck failed, error %lu\n", status);
68 		return status;
69 	}
70 
71 	status = formats.GetFormatFor(desc, out_format);
72 	if (status) {
73 		printf("formats.GetFormatFor failed, error %lu\n", status);
74 		return status;
75 	}
76 
77 	return B_OK;
78 }
79 
80 
81 
82 static status_t
83 GetHeaderFormatDtsAudio(media_format *out_format, const uint8 *header, size_t size)
84 {
85 	printf("GetHeaderFormatDtsAudio: unsupported\n");
86 	return B_ERROR;
87 }
88 
89 
90 static status_t
91 GetHeaderFormatLpcmAudio(media_format *out_format, const uint8 *header, size_t size)
92 {
93 	printf("GetHeaderFormatLpcmAudio: unsupported\n");
94 	return B_ERROR;
95 }
96 
97 
98 static status_t
99 GetHeaderFormatPrivateStream(media_format *out_format, const uint8 *header, size_t size)
100 {
101 	printf("GetHeaderFormatPrivateStream: unsupported, assuming AC3\n");
102 	return GetHeaderFormatAc3Audio(out_format, header, size);
103 }
104 
105 
106 static status_t
107 GetHeaderFormatMpegAudio(media_format *out_format, const uint8 *header, size_t size)
108 {
109 	printf("GetHeaderFormatMpegAudio\n");
110 
111 	status_t status;
112 	media_format_description desc;
113 //	desc.family = B_MPEG_FORMAT_FAMILY;
114 //	desc.u.mpeg.id = B_MPEG_2_AUDIO_LAYER_2;
115 	desc.family = B_MISC_FORMAT_FAMILY;
116 	desc.u.misc.file_format = 'ffmp';
117 	desc.u.misc.codec = CODEC_ID_MP3;
118 
119 
120 	BMediaFormats formats;
121 	status = formats.InitCheck();
122 	if (status) {
123 		printf("formats.InitCheck failed, error %lu\n", status);
124 		return status;
125 	}
126 
127 	status = formats.GetFormatFor(desc, out_format);
128 	if (status) {
129 		printf("formats.GetFormatFor failed, error %lu\n", status);
130 		return status;
131 	}
132 
133 	out_format->u.encoded_audio.output.frame_rate = 48000;
134 	out_format->u.encoded_audio.output.channel_count = 2;
135 	out_format->u.encoded_audio.output.buffer_size = 1024;
136 	return B_OK;
137 }
138 
139 
140 static status_t
141 GetHeaderFormatMpegVideo(media_format *out_format, const uint8 *header, size_t size)
142 {
143 	printf("GetHeaderFormatMpegVideo\n");
144 
145 	status_t status;
146 	media_format_description desc;
147 //	desc.family = B_MPEG_FORMAT_FAMILY;
148 //	desc.u.mpeg.id = B_MPEG_2_VIDEO;
149 	desc.family = B_MISC_FORMAT_FAMILY;
150 	desc.u.misc.file_format = 'ffmp';
151 	desc.u.misc.codec = CODEC_ID_MPEG2VIDEO;
152 
153 	BMediaFormats formats;
154 	status = formats.InitCheck();
155 	if (status) {
156 		printf("formats.InitCheck failed, error %lu\n", status);
157 		return status;
158 	}
159 
160 	status = formats.GetFormatFor(desc, out_format);
161 	if (status) {
162 		printf("formats.GetFormatFor failed, error %lu\n", status);
163 		return status;
164 	}
165 
166 	return B_OK;
167 }
168 
169 
170 status_t
171 GetHeaderFormat(media_format *out_format, const void *header, size_t size, int stream_id)
172 {
173 	const uint8 *h = (const uint8 *)header;
174 	status_t status;
175 
176 	printf("GetHeaderFormat: stream_id %02x\n", stream_id);
177 	printf("inner frame header: "
178 		   "%02x %02x %02x %02x %02x %02x %02x %02x "
179 		   "%02x %02x %02x %02x %02x %02x %02x %02x\n",
180 		   h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
181 		   h[8], h[9], h[10], h[11], h[12], h[13], h[14], h[15]);
182 
183 	if (stream_id >= 0x80 && stream_id <= 0x87)
184 		status = GetHeaderFormatAc3Audio(out_format, h, size);
185 	else if (stream_id >= 0x88 && stream_id <= 0x8F)
186 		status = GetHeaderFormatDtsAudio(out_format, h, size);
187 	else if (stream_id >= 0xA0 && stream_id <= 0xA7)
188 		status = GetHeaderFormatLpcmAudio(out_format, h, size);
189 	else if (stream_id == 0xBD)
190 		status = GetHeaderFormatPrivateStream(out_format, h, size);
191 	else if (stream_id >= 0xC0 && stream_id <= 0xDF)
192 		status = GetHeaderFormatMpegAudio(out_format, h, size);
193 	else if (stream_id >= 0xE0 && stream_id <= 0xEF)
194 		status = GetHeaderFormatMpegVideo(out_format, h, size);
195 	else {
196 		printf("GetHeaderFormat: don't know what this stream_id means\n");
197 		status = B_ERROR;
198 	}
199 
200 	if (status != B_OK) {
201 		printf("GetHeaderFormat: failed!\n");
202 	} else {
203 		printf("GetHeaderFormat: out_format ");
204 		PrintFormat(*out_format);
205 	}
206 	return status;
207 }
208