1 | /*
|
---|
2 | * MPEG2 transport stream (aka DVB) mux
|
---|
3 | * Copyright (c) 2003 Fabrice Bellard.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 | #include "avformat.h"
|
---|
20 | #include "crc.h"
|
---|
21 | #include "mpegts.h"
|
---|
22 |
|
---|
23 | /* write DVB SI sections */
|
---|
24 |
|
---|
25 | #ifdef CONFIG_MUXERS
|
---|
26 | /*********************************************/
|
---|
27 | /* mpegts section writer */
|
---|
28 |
|
---|
29 | typedef struct MpegTSSection {
|
---|
30 | int pid;
|
---|
31 | int cc;
|
---|
32 | void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
|
---|
33 | void *opaque;
|
---|
34 | } MpegTSSection;
|
---|
35 |
|
---|
36 | /* NOTE: 4 bytes must be left at the end for the crc32 */
|
---|
37 | static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
|
---|
38 | {
|
---|
39 | unsigned int crc;
|
---|
40 | unsigned char packet[TS_PACKET_SIZE];
|
---|
41 | const unsigned char *buf_ptr;
|
---|
42 | unsigned char *q;
|
---|
43 | int first, b, len1, left;
|
---|
44 |
|
---|
45 | crc = bswap_32(av_crc(av_crc04C11DB7, -1, buf, len - 4));
|
---|
46 | buf[len - 4] = (crc >> 24) & 0xff;
|
---|
47 | buf[len - 3] = (crc >> 16) & 0xff;
|
---|
48 | buf[len - 2] = (crc >> 8) & 0xff;
|
---|
49 | buf[len - 1] = (crc) & 0xff;
|
---|
50 |
|
---|
51 | /* send each packet */
|
---|
52 | buf_ptr = buf;
|
---|
53 | while (len > 0) {
|
---|
54 | first = (buf == buf_ptr);
|
---|
55 | q = packet;
|
---|
56 | *q++ = 0x47;
|
---|
57 | b = (s->pid >> 8);
|
---|
58 | if (first)
|
---|
59 | b |= 0x40;
|
---|
60 | *q++ = b;
|
---|
61 | *q++ = s->pid;
|
---|
62 | s->cc = (s->cc + 1) & 0xf;
|
---|
63 | *q++ = 0x10 | s->cc;
|
---|
64 | if (first)
|
---|
65 | *q++ = 0; /* 0 offset */
|
---|
66 | len1 = TS_PACKET_SIZE - (q - packet);
|
---|
67 | if (len1 > len)
|
---|
68 | len1 = len;
|
---|
69 | memcpy(q, buf_ptr, len1);
|
---|
70 | q += len1;
|
---|
71 | /* add known padding data */
|
---|
72 | left = TS_PACKET_SIZE - (q - packet);
|
---|
73 | if (left > 0)
|
---|
74 | memset(q, 0xff, left);
|
---|
75 |
|
---|
76 | s->write_packet(s, packet);
|
---|
77 |
|
---|
78 | buf_ptr += len1;
|
---|
79 | len -= len1;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | static inline void put16(uint8_t **q_ptr, int val)
|
---|
84 | {
|
---|
85 | uint8_t *q;
|
---|
86 | q = *q_ptr;
|
---|
87 | *q++ = val >> 8;
|
---|
88 | *q++ = val;
|
---|
89 | *q_ptr = q;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
|
---|
93 | int version, int sec_num, int last_sec_num,
|
---|
94 | uint8_t *buf, int len)
|
---|
95 | {
|
---|
96 | uint8_t section[1024], *q;
|
---|
97 | unsigned int tot_len;
|
---|
98 |
|
---|
99 | tot_len = 3 + 5 + len + 4;
|
---|
100 | /* check if not too big */
|
---|
101 | if (tot_len > 1024)
|
---|
102 | return -1;
|
---|
103 |
|
---|
104 | q = section;
|
---|
105 | *q++ = tid;
|
---|
106 | put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
|
---|
107 | put16(&q, id);
|
---|
108 | *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
|
---|
109 | *q++ = sec_num;
|
---|
110 | *q++ = last_sec_num;
|
---|
111 | memcpy(q, buf, len);
|
---|
112 |
|
---|
113 | mpegts_write_section(s, section, tot_len);
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*********************************************/
|
---|
118 | /* mpegts writer */
|
---|
119 |
|
---|
120 | #define DEFAULT_PMT_START_PID 0x1000
|
---|
121 | #define DEFAULT_START_PID 0x0100
|
---|
122 | #define DEFAULT_PROVIDER_NAME "FFmpeg"
|
---|
123 | #define DEFAULT_SERVICE_NAME "Service01"
|
---|
124 |
|
---|
125 | /* default network id, transport stream and service identifiers */
|
---|
126 | #define DEFAULT_ONID 0x0001
|
---|
127 | #define DEFAULT_TSID 0x0001
|
---|
128 | #define DEFAULT_SID 0x0001
|
---|
129 |
|
---|
130 | /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
|
---|
131 | #define DEFAULT_PES_HEADER_FREQ 16
|
---|
132 | #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
|
---|
133 |
|
---|
134 | /* we retransmit the SI info at this rate */
|
---|
135 | #define SDT_RETRANS_TIME 500
|
---|
136 | #define PAT_RETRANS_TIME 100
|
---|
137 | #define PCR_RETRANS_TIME 20
|
---|
138 |
|
---|
139 | typedef struct MpegTSWriteStream {
|
---|
140 | struct MpegTSService *service;
|
---|
141 | int pid; /* stream associated pid */
|
---|
142 | int cc;
|
---|
143 | int payload_index;
|
---|
144 | int64_t payload_pts;
|
---|
145 | uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
|
---|
146 | } MpegTSWriteStream;
|
---|
147 |
|
---|
148 | typedef struct MpegTSService {
|
---|
149 | MpegTSSection pmt; /* MPEG2 pmt table context */
|
---|
150 | int sid; /* service ID */
|
---|
151 | char *name;
|
---|
152 | char *provider_name;
|
---|
153 | int pcr_pid;
|
---|
154 | int pcr_packet_count;
|
---|
155 | int pcr_packet_freq;
|
---|
156 | } MpegTSService;
|
---|
157 |
|
---|
158 | typedef struct MpegTSWrite {
|
---|
159 | MpegTSSection pat; /* MPEG2 pat table */
|
---|
160 | MpegTSSection sdt; /* MPEG2 sdt table context */
|
---|
161 | MpegTSService **services;
|
---|
162 | int sdt_packet_count;
|
---|
163 | int sdt_packet_freq;
|
---|
164 | int pat_packet_count;
|
---|
165 | int pat_packet_freq;
|
---|
166 | int nb_services;
|
---|
167 | int onid;
|
---|
168 | int tsid;
|
---|
169 | } MpegTSWrite;
|
---|
170 |
|
---|
171 | static void mpegts_write_pat(AVFormatContext *s)
|
---|
172 | {
|
---|
173 | MpegTSWrite *ts = s->priv_data;
|
---|
174 | MpegTSService *service;
|
---|
175 | uint8_t data[1012], *q;
|
---|
176 | int i;
|
---|
177 |
|
---|
178 | q = data;
|
---|
179 | for(i = 0; i < ts->nb_services; i++) {
|
---|
180 | service = ts->services[i];
|
---|
181 | put16(&q, service->sid);
|
---|
182 | put16(&q, 0xe000 | service->pmt.pid);
|
---|
183 | }
|
---|
184 | mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
|
---|
185 | data, q - data);
|
---|
186 | }
|
---|
187 |
|
---|
188 | static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
|
---|
189 | {
|
---|
190 | // MpegTSWrite *ts = s->priv_data;
|
---|
191 | uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
|
---|
192 | int val, stream_type, i;
|
---|
193 |
|
---|
194 | q = data;
|
---|
195 | put16(&q, 0xe000 | service->pcr_pid);
|
---|
196 |
|
---|
197 | program_info_length_ptr = q;
|
---|
198 | q += 2; /* patched after */
|
---|
199 |
|
---|
200 | /* put program info here */
|
---|
201 |
|
---|
202 | val = 0xf000 | (q - program_info_length_ptr - 2);
|
---|
203 | program_info_length_ptr[0] = val >> 8;
|
---|
204 | program_info_length_ptr[1] = val;
|
---|
205 |
|
---|
206 | for(i = 0; i < s->nb_streams; i++) {
|
---|
207 | AVStream *st = s->streams[i];
|
---|
208 | MpegTSWriteStream *ts_st = st->priv_data;
|
---|
209 | switch(st->codec->codec_id) {
|
---|
210 | case CODEC_ID_MPEG1VIDEO:
|
---|
211 | case CODEC_ID_MPEG2VIDEO:
|
---|
212 | stream_type = STREAM_TYPE_VIDEO_MPEG2;
|
---|
213 | break;
|
---|
214 | case CODEC_ID_MPEG4:
|
---|
215 | stream_type = STREAM_TYPE_VIDEO_MPEG4;
|
---|
216 | break;
|
---|
217 | case CODEC_ID_H264:
|
---|
218 | stream_type = STREAM_TYPE_VIDEO_H264;
|
---|
219 | break;
|
---|
220 | case CODEC_ID_MP2:
|
---|
221 | case CODEC_ID_MP3:
|
---|
222 | stream_type = STREAM_TYPE_AUDIO_MPEG1;
|
---|
223 | break;
|
---|
224 | case CODEC_ID_AAC:
|
---|
225 | stream_type = STREAM_TYPE_AUDIO_AAC;
|
---|
226 | break;
|
---|
227 | case CODEC_ID_AC3:
|
---|
228 | stream_type = STREAM_TYPE_AUDIO_AC3;
|
---|
229 | break;
|
---|
230 | default:
|
---|
231 | stream_type = STREAM_TYPE_PRIVATE_DATA;
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | *q++ = stream_type;
|
---|
235 | put16(&q, 0xe000 | ts_st->pid);
|
---|
236 | desc_length_ptr = q;
|
---|
237 | q += 2; /* patched after */
|
---|
238 |
|
---|
239 | /* write optional descriptors here */
|
---|
240 | switch(st->codec->codec_type) {
|
---|
241 | case CODEC_TYPE_AUDIO:
|
---|
242 | if (strlen(st->language) == 3) {
|
---|
243 | *q++ = 0x0a; /* ISO 639 language descriptor */
|
---|
244 | *q++ = 4;
|
---|
245 | *q++ = st->language[0];
|
---|
246 | *q++ = st->language[1];
|
---|
247 | *q++ = st->language[2];
|
---|
248 | *q++ = 0; /* undefined type */
|
---|
249 | }
|
---|
250 | break;
|
---|
251 | case CODEC_TYPE_SUBTITLE:
|
---|
252 | {
|
---|
253 | const char *language;
|
---|
254 | language = st->language;
|
---|
255 | if (strlen(language) != 3)
|
---|
256 | language = "eng";
|
---|
257 | *q++ = 0x59;
|
---|
258 | *q++ = 8;
|
---|
259 | *q++ = language[0];
|
---|
260 | *q++ = language[1];
|
---|
261 | *q++ = language[2];
|
---|
262 | *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
|
---|
263 | put16(&q, 1); /* page id */
|
---|
264 | put16(&q, 1); /* ancillary page id */
|
---|
265 | }
|
---|
266 | break;
|
---|
267 | }
|
---|
268 |
|
---|
269 | val = 0xf000 | (q - desc_length_ptr - 2);
|
---|
270 | desc_length_ptr[0] = val >> 8;
|
---|
271 | desc_length_ptr[1] = val;
|
---|
272 | }
|
---|
273 | mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
|
---|
274 | data, q - data);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* NOTE: str == NULL is accepted for an empty string */
|
---|
278 | static void putstr8(uint8_t **q_ptr, const char *str)
|
---|
279 | {
|
---|
280 | uint8_t *q;
|
---|
281 | int len;
|
---|
282 |
|
---|
283 | q = *q_ptr;
|
---|
284 | if (!str)
|
---|
285 | len = 0;
|
---|
286 | else
|
---|
287 | len = strlen(str);
|
---|
288 | *q++ = len;
|
---|
289 | memcpy(q, str, len);
|
---|
290 | q += len;
|
---|
291 | *q_ptr = q;
|
---|
292 | }
|
---|
293 |
|
---|
294 | static void mpegts_write_sdt(AVFormatContext *s)
|
---|
295 | {
|
---|
296 | MpegTSWrite *ts = s->priv_data;
|
---|
297 | MpegTSService *service;
|
---|
298 | uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
|
---|
299 | int i, running_status, free_ca_mode, val;
|
---|
300 |
|
---|
301 | q = data;
|
---|
302 | put16(&q, ts->onid);
|
---|
303 | *q++ = 0xff;
|
---|
304 | for(i = 0; i < ts->nb_services; i++) {
|
---|
305 | service = ts->services[i];
|
---|
306 | put16(&q, service->sid);
|
---|
307 | *q++ = 0xfc | 0x00; /* currently no EIT info */
|
---|
308 | desc_list_len_ptr = q;
|
---|
309 | q += 2;
|
---|
310 | running_status = 4; /* running */
|
---|
311 | free_ca_mode = 0;
|
---|
312 |
|
---|
313 | /* write only one descriptor for the service name and provider */
|
---|
314 | *q++ = 0x48;
|
---|
315 | desc_len_ptr = q;
|
---|
316 | q++;
|
---|
317 | *q++ = 0x01; /* digital television service */
|
---|
318 | putstr8(&q, service->provider_name);
|
---|
319 | putstr8(&q, service->name);
|
---|
320 | desc_len_ptr[0] = q - desc_len_ptr - 1;
|
---|
321 |
|
---|
322 | /* fill descriptor length */
|
---|
323 | val = (running_status << 13) | (free_ca_mode << 12) |
|
---|
324 | (q - desc_list_len_ptr - 2);
|
---|
325 | desc_list_len_ptr[0] = val >> 8;
|
---|
326 | desc_list_len_ptr[1] = val;
|
---|
327 | }
|
---|
328 | mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
|
---|
329 | data, q - data);
|
---|
330 | }
|
---|
331 |
|
---|
332 | static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
|
---|
333 | int sid,
|
---|
334 | const char *provider_name,
|
---|
335 | const char *name)
|
---|
336 | {
|
---|
337 | MpegTSService *service;
|
---|
338 |
|
---|
339 | service = av_mallocz(sizeof(MpegTSService));
|
---|
340 | if (!service)
|
---|
341 | return NULL;
|
---|
342 | service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
|
---|
343 | service->sid = sid;
|
---|
344 | service->provider_name = av_strdup(provider_name);
|
---|
345 | service->name = av_strdup(name);
|
---|
346 | service->pcr_pid = 0x1fff;
|
---|
347 | dynarray_add(&ts->services, &ts->nb_services, service);
|
---|
348 | return service;
|
---|
349 | }
|
---|
350 |
|
---|
351 | static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
|
---|
352 | {
|
---|
353 | AVFormatContext *ctx = s->opaque;
|
---|
354 | put_buffer(&ctx->pb, packet, TS_PACKET_SIZE);
|
---|
355 | }
|
---|
356 |
|
---|
357 | static int mpegts_write_header(AVFormatContext *s)
|
---|
358 | {
|
---|
359 | MpegTSWrite *ts = s->priv_data;
|
---|
360 | MpegTSWriteStream *ts_st;
|
---|
361 | MpegTSService *service;
|
---|
362 | AVStream *st;
|
---|
363 | int i, total_bit_rate;
|
---|
364 | const char *service_name;
|
---|
365 |
|
---|
366 | ts->tsid = DEFAULT_TSID;
|
---|
367 | ts->onid = DEFAULT_ONID;
|
---|
368 | /* allocate a single DVB service */
|
---|
369 | service_name = s->title;
|
---|
370 | if (service_name[0] == '\0')
|
---|
371 | service_name = DEFAULT_SERVICE_NAME;
|
---|
372 | service = mpegts_add_service(ts, DEFAULT_SID,
|
---|
373 | DEFAULT_PROVIDER_NAME, service_name);
|
---|
374 | service->pmt.write_packet = section_write_packet;
|
---|
375 | service->pmt.opaque = s;
|
---|
376 |
|
---|
377 | ts->pat.pid = PAT_PID;
|
---|
378 | ts->pat.cc = 0;
|
---|
379 | ts->pat.write_packet = section_write_packet;
|
---|
380 | ts->pat.opaque = s;
|
---|
381 |
|
---|
382 | ts->sdt.pid = SDT_PID;
|
---|
383 | ts->sdt.cc = 0;
|
---|
384 | ts->sdt.write_packet = section_write_packet;
|
---|
385 | ts->sdt.opaque = s;
|
---|
386 |
|
---|
387 | /* assign pids to each stream */
|
---|
388 | total_bit_rate = 0;
|
---|
389 | for(i = 0;i < s->nb_streams; i++) {
|
---|
390 | st = s->streams[i];
|
---|
391 | ts_st = av_mallocz(sizeof(MpegTSWriteStream));
|
---|
392 | if (!ts_st)
|
---|
393 | goto fail;
|
---|
394 | st->priv_data = ts_st;
|
---|
395 | ts_st->service = service;
|
---|
396 | ts_st->pid = DEFAULT_START_PID + i;
|
---|
397 | ts_st->payload_pts = AV_NOPTS_VALUE;
|
---|
398 | /* update PCR pid by using the first video stream */
|
---|
399 | if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
|
---|
400 | service->pcr_pid == 0x1fff)
|
---|
401 | service->pcr_pid = ts_st->pid;
|
---|
402 | total_bit_rate += st->codec->bit_rate;
|
---|
403 | }
|
---|
404 |
|
---|
405 | /* if no video stream, use the first stream as PCR */
|
---|
406 | if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
|
---|
407 | ts_st = s->streams[0]->priv_data;
|
---|
408 | service->pcr_pid = ts_st->pid;
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (total_bit_rate <= 8 * 1024)
|
---|
412 | total_bit_rate = 8 * 1024;
|
---|
413 | service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
|
---|
414 | (TS_PACKET_SIZE * 8 * 1000);
|
---|
415 | ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
|
---|
416 | (TS_PACKET_SIZE * 8 * 1000);
|
---|
417 | ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
|
---|
418 | (TS_PACKET_SIZE * 8 * 1000);
|
---|
419 | #if 0
|
---|
420 | printf("%d %d %d\n",
|
---|
421 | total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
|
---|
422 | #endif
|
---|
423 |
|
---|
424 | /* write info at the start of the file, so that it will be fast to
|
---|
425 | find them */
|
---|
426 | mpegts_write_sdt(s);
|
---|
427 | mpegts_write_pat(s);
|
---|
428 | for(i = 0; i < ts->nb_services; i++) {
|
---|
429 | mpegts_write_pmt(s, ts->services[i]);
|
---|
430 | }
|
---|
431 | put_flush_packet(&s->pb);
|
---|
432 |
|
---|
433 | return 0;
|
---|
434 |
|
---|
435 | fail:
|
---|
436 | for(i = 0;i < s->nb_streams; i++) {
|
---|
437 | st = s->streams[i];
|
---|
438 | av_free(st->priv_data);
|
---|
439 | }
|
---|
440 | return -1;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /* send SDT, PAT and PMT tables regulary */
|
---|
444 | static void retransmit_si_info(AVFormatContext *s)
|
---|
445 | {
|
---|
446 | MpegTSWrite *ts = s->priv_data;
|
---|
447 | int i;
|
---|
448 |
|
---|
449 | if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
|
---|
450 | ts->sdt_packet_count = 0;
|
---|
451 | mpegts_write_sdt(s);
|
---|
452 | }
|
---|
453 | if (++ts->pat_packet_count == ts->pat_packet_freq) {
|
---|
454 | ts->pat_packet_count = 0;
|
---|
455 | mpegts_write_pat(s);
|
---|
456 | for(i = 0; i < ts->nb_services; i++) {
|
---|
457 | mpegts_write_pmt(s, ts->services[i]);
|
---|
458 | }
|
---|
459 | }
|
---|
460 | }
|
---|
461 |
|
---|
462 | /* NOTE: pes_data contains all the PES packet */
|
---|
463 | static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
|
---|
464 | const uint8_t *payload, int payload_size,
|
---|
465 | int64_t pts)
|
---|
466 | {
|
---|
467 | MpegTSWriteStream *ts_st = st->priv_data;
|
---|
468 | uint8_t buf[TS_PACKET_SIZE];
|
---|
469 | uint8_t *q;
|
---|
470 | int val, is_start, len, header_len, write_pcr, private_code;
|
---|
471 | int afc_len, stuffing_len;
|
---|
472 | int64_t pcr = -1; /* avoid warning */
|
---|
473 |
|
---|
474 | is_start = 1;
|
---|
475 | while (payload_size > 0) {
|
---|
476 | retransmit_si_info(s);
|
---|
477 |
|
---|
478 | write_pcr = 0;
|
---|
479 | if (ts_st->pid == ts_st->service->pcr_pid) {
|
---|
480 | ts_st->service->pcr_packet_count++;
|
---|
481 | if (ts_st->service->pcr_packet_count >=
|
---|
482 | ts_st->service->pcr_packet_freq) {
|
---|
483 | ts_st->service->pcr_packet_count = 0;
|
---|
484 | write_pcr = 1;
|
---|
485 | /* XXX: this is incorrect, but at least we have a PCR
|
---|
486 | value */
|
---|
487 | pcr = pts;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | /* prepare packet header */
|
---|
492 | q = buf;
|
---|
493 | *q++ = 0x47;
|
---|
494 | val = (ts_st->pid >> 8);
|
---|
495 | if (is_start)
|
---|
496 | val |= 0x40;
|
---|
497 | *q++ = val;
|
---|
498 | *q++ = ts_st->pid;
|
---|
499 | *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
|
---|
500 | ts_st->cc = (ts_st->cc + 1) & 0xf;
|
---|
501 | if (write_pcr) {
|
---|
502 | *q++ = 7; /* AFC length */
|
---|
503 | *q++ = 0x10; /* flags: PCR present */
|
---|
504 | *q++ = pcr >> 25;
|
---|
505 | *q++ = pcr >> 17;
|
---|
506 | *q++ = pcr >> 9;
|
---|
507 | *q++ = pcr >> 1;
|
---|
508 | *q++ = (pcr & 1) << 7;
|
---|
509 | *q++ = 0;
|
---|
510 | }
|
---|
511 | if (is_start) {
|
---|
512 | /* write PES header */
|
---|
513 | *q++ = 0x00;
|
---|
514 | *q++ = 0x00;
|
---|
515 | *q++ = 0x01;
|
---|
516 | private_code = 0;
|
---|
517 | if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
---|
518 | *q++ = 0xe0;
|
---|
519 | } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
|
---|
520 | (st->codec->codec_id == CODEC_ID_MP2 ||
|
---|
521 | st->codec->codec_id == CODEC_ID_MP3)) {
|
---|
522 | *q++ = 0xc0;
|
---|
523 | } else {
|
---|
524 | *q++ = 0xbd;
|
---|
525 | if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
|
---|
526 | private_code = 0x20;
|
---|
527 | }
|
---|
528 | }
|
---|
529 | if (pts != AV_NOPTS_VALUE)
|
---|
530 | header_len = 8;
|
---|
531 | else
|
---|
532 | header_len = 3;
|
---|
533 | if (private_code != 0)
|
---|
534 | header_len++;
|
---|
535 | len = payload_size + header_len;
|
---|
536 | *q++ = len >> 8;
|
---|
537 | *q++ = len;
|
---|
538 | val = 0x80;
|
---|
539 | /* data alignment indicator is required for subtitle data */
|
---|
540 | if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
|
---|
541 | val |= 0x04;
|
---|
542 | *q++ = val;
|
---|
543 | if (pts != AV_NOPTS_VALUE) {
|
---|
544 | *q++ = 0x80; /* PTS only */
|
---|
545 | *q++ = 0x05; /* header len */
|
---|
546 | val = (0x02 << 4) |
|
---|
547 | (((pts >> 30) & 0x07) << 1) | 1;
|
---|
548 | *q++ = val;
|
---|
549 | val = (((pts >> 15) & 0x7fff) << 1) | 1;
|
---|
550 | *q++ = val >> 8;
|
---|
551 | *q++ = val;
|
---|
552 | val = (((pts) & 0x7fff) << 1) | 1;
|
---|
553 | *q++ = val >> 8;
|
---|
554 | *q++ = val;
|
---|
555 | } else {
|
---|
556 | *q++ = 0x00;
|
---|
557 | *q++ = 0x00;
|
---|
558 | }
|
---|
559 | if (private_code != 0)
|
---|
560 | *q++ = private_code;
|
---|
561 | is_start = 0;
|
---|
562 | }
|
---|
563 | /* header size */
|
---|
564 | header_len = q - buf;
|
---|
565 | /* data len */
|
---|
566 | len = TS_PACKET_SIZE - header_len;
|
---|
567 | if (len > payload_size)
|
---|
568 | len = payload_size;
|
---|
569 | stuffing_len = TS_PACKET_SIZE - header_len - len;
|
---|
570 | if (stuffing_len > 0) {
|
---|
571 | /* add stuffing with AFC */
|
---|
572 | if (buf[3] & 0x20) {
|
---|
573 | /* stuffing already present: increase its size */
|
---|
574 | afc_len = buf[4] + 1;
|
---|
575 | memmove(buf + 4 + afc_len + stuffing_len,
|
---|
576 | buf + 4 + afc_len,
|
---|
577 | header_len - (4 + afc_len));
|
---|
578 | buf[4] += stuffing_len;
|
---|
579 | memset(buf + 4 + afc_len, 0xff, stuffing_len);
|
---|
580 | } else {
|
---|
581 | /* add stuffing */
|
---|
582 | memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
|
---|
583 | buf[3] |= 0x20;
|
---|
584 | buf[4] = stuffing_len - 1;
|
---|
585 | if (stuffing_len >= 2) {
|
---|
586 | buf[5] = 0x00;
|
---|
587 | memset(buf + 6, 0xff, stuffing_len - 2);
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 | memcpy(buf + TS_PACKET_SIZE - len, payload, len);
|
---|
592 | payload += len;
|
---|
593 | payload_size -= len;
|
---|
594 | put_buffer(&s->pb, buf, TS_PACKET_SIZE);
|
---|
595 | }
|
---|
596 | put_flush_packet(&s->pb);
|
---|
597 | }
|
---|
598 |
|
---|
599 | static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
600 | {
|
---|
601 | AVStream *st = s->streams[pkt->stream_index];
|
---|
602 | int size= pkt->size;
|
---|
603 | uint8_t *buf= pkt->data;
|
---|
604 | MpegTSWriteStream *ts_st = st->priv_data;
|
---|
605 | int len, max_payload_size;
|
---|
606 |
|
---|
607 | if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
|
---|
608 | /* for subtitle, a single PES packet must be generated */
|
---|
609 | mpegts_write_pes(s, st, buf, size, pkt->pts);
|
---|
610 | return 0;
|
---|
611 | }
|
---|
612 |
|
---|
613 | max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
|
---|
614 | while (size > 0) {
|
---|
615 | len = max_payload_size - ts_st->payload_index;
|
---|
616 | if (len > size)
|
---|
617 | len = size;
|
---|
618 | memcpy(ts_st->payload + ts_st->payload_index, buf, len);
|
---|
619 | buf += len;
|
---|
620 | size -= len;
|
---|
621 | ts_st->payload_index += len;
|
---|
622 | if (ts_st->payload_pts == AV_NOPTS_VALUE)
|
---|
623 | ts_st->payload_pts = pkt->pts;
|
---|
624 | if (ts_st->payload_index >= max_payload_size) {
|
---|
625 | mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
|
---|
626 | ts_st->payload_pts);
|
---|
627 | ts_st->payload_pts = AV_NOPTS_VALUE;
|
---|
628 | ts_st->payload_index = 0;
|
---|
629 | }
|
---|
630 | }
|
---|
631 | return 0;
|
---|
632 | }
|
---|
633 |
|
---|
634 | static int mpegts_write_end(AVFormatContext *s)
|
---|
635 | {
|
---|
636 | MpegTSWrite *ts = s->priv_data;
|
---|
637 | MpegTSWriteStream *ts_st;
|
---|
638 | MpegTSService *service;
|
---|
639 | AVStream *st;
|
---|
640 | int i;
|
---|
641 |
|
---|
642 | /* flush current packets */
|
---|
643 | for(i = 0; i < s->nb_streams; i++) {
|
---|
644 | st = s->streams[i];
|
---|
645 | ts_st = st->priv_data;
|
---|
646 | if (ts_st->payload_index > 0) {
|
---|
647 | mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
|
---|
648 | ts_st->payload_pts);
|
---|
649 | }
|
---|
650 | }
|
---|
651 | put_flush_packet(&s->pb);
|
---|
652 |
|
---|
653 | for(i = 0; i < ts->nb_services; i++) {
|
---|
654 | service = ts->services[i];
|
---|
655 | av_freep(&service->provider_name);
|
---|
656 | av_freep(&service->name);
|
---|
657 | av_free(service);
|
---|
658 | }
|
---|
659 | av_free(ts->services);
|
---|
660 |
|
---|
661 | return 0;
|
---|
662 | }
|
---|
663 |
|
---|
664 | AVOutputFormat mpegts_muxer = {
|
---|
665 | "mpegts",
|
---|
666 | "MPEG2 transport stream format",
|
---|
667 | "video/x-mpegts",
|
---|
668 | "ts",
|
---|
669 | sizeof(MpegTSWrite),
|
---|
670 | CODEC_ID_MP2,
|
---|
671 | CODEC_ID_MPEG2VIDEO,
|
---|
672 | mpegts_write_header,
|
---|
673 | mpegts_write_packet,
|
---|
674 | mpegts_write_end,
|
---|
675 | };
|
---|
676 | #endif // CONFIG_MUXERS
|
---|