MythTV  master
multiplex.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstring>
3 #include <unistd.h>
4 
5 #include "multiplex.h"
6 #include "ts.h"
7 #include "mythlogging.h"
8 
9 static int buffers_filled(multiplex_t *mx)
10 {
11  int aavail=0;
12  int vavail = ring_avail(mx->index_vrbuffer)/sizeof(index_unit);
13 
14  for (int i=0; i<mx->extcnt;i++) {
15  aavail += ring_avail(&mx->index_extrbuffer[i])/
16  sizeof(index_unit);
17  }
18 
19  if (aavail+vavail) return ((aavail+vavail));
20  return 0;
21 }
22 
23 static int use_video(uint64_t vpts, extdata_t *ext, const int *aok, int n)
24 {
25  for (int i=0; i < n; i++)
26  if(aok[i] && ptscmp(vpts,ext[i].pts) > 0)
27  return 0;
28  return 1;
29 }
30 static int which_ext(extdata_t *ext, const int *aok, int n)
31 {
32  int started = 0;
33  int pos = -1;
34  uint64_t tmppts = 0;
35  for (int i=0; i < n; i++) {
36  if(aok[i]){
37  if(! started){
38  started=1;
39  tmppts=ext[i].pts;
40  pos = i;
41  } else if(ptscmp(tmppts, ext[i].pts) > 0) {
42  tmppts = ext[i].pts;
43  pos = i;
44  }
45  }
46  }
47  return pos;
48 }
49 
51 {
52  if (!ring_avail(mx->index_vrbuffer) && mx->finish) return 0;
53 
54  while (ring_avail(mx->index_vrbuffer) < sizeof(index_unit)) {
55  if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
56  LOG(VB_GENERAL, LOG_ERR,
57  "error in peek next video unit");
58  return 0;
59  }
60  }
61 
62  ring_peek(mx->index_vrbuffer, (uint8_t *)viu, sizeof(index_unit),0);
63 #ifdef OUT_DEBUG
64  LOG(VB_GENERAL, LOG_DEBUG,
65  QString("video index start: %1 stop: %2 (%3) rpos: %4\n")
66  .arg(viu->start).arg(viu->start+viu->length)
67  .arg(viu->length).arg(ring_rpos(mx->vrbuffer)));
68 #endif
69 
70  return 1;
71 }
72 
74 {
75  index_unit nviu {};
76  if (!ring_avail(mx->index_vrbuffer) && mx->finish) return 0;
77 
78  while (ring_avail(mx->index_vrbuffer) < sizeof(index_unit)) {
79  if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
80  LOG(VB_GENERAL, LOG_ERR,
81  "error in get next video unit");
82  return 0;
83  }
84  }
85 
86  ring_read(mx->index_vrbuffer, (uint8_t *)viu, sizeof(index_unit));
87 #ifdef OUT_DEBUG
88  LOG(VB_GENERAL, LOG_INFO,
89  QString("video index start: %1 stop: %2 (%3) rpos: %4\n")
90  .arg(viu->start).arg(viu->start+viu->length)
91  .arg(viu->length).arg(ring_rpos(mx->vrbuffer)));
92 #endif
93  if(! peek_next_video_unit(mx, &nviu))
94  return 1;
95  //left-shift by 8 to increase precision
96  viu->ptsrate = (uptsdiff(nviu.dts, viu->dts) << 8) / viu->length;
97  return 1;
98 }
99 
100 static int peek_next_ext_unit(multiplex_t *mx, index_unit *extiu, int i)
101 {
102  if (!ring_avail(&mx->index_extrbuffer[i]) && mx->finish) return 0;
103 
104  while (ring_avail(&mx->index_extrbuffer[i]) < sizeof(index_unit)) {
105  if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
106  LOG(VB_GENERAL, LOG_ERR,
107  "error in peek next video unit");
108  return 0;
109  }
110  }
111 
112  ring_peek(&mx->index_extrbuffer[i], (uint8_t *)extiu,
113  sizeof(index_unit),0);
114 #ifdef OUT_DEBUG
115  LOG(VB_GENERAL, LOG_DEBUG,
116  QString("ext index start: %1 stop: %2 (%3) rpos: %4")
117  .arg(extiu->start).arg(extiu->start+extiu->length)
118  .arg(extiu->length).arg(ring_rpos(mx->extrbuffer)));
119 #endif
120 
121  return 1;
122 }
123 
124 static int get_next_ext_unit(multiplex_t *mx, index_unit *extiu, int i)
125 {
126  index_unit niu {};
127  index_unit *piu = extiu;
128  int length = 0;
129  int j = 0;
130  for(j = 0; j < mx->ext[i].frmperpkt; j++) {
131  if (!ring_avail(&mx->index_extrbuffer[i]) && mx->finish)
132  break;
133 
134  while(ring_avail(&mx->index_extrbuffer[i]) < sizeof(index_unit)) {
135  if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
136  LOG(VB_GENERAL, LOG_ERR,
137  "error in get next ext unit");
138  break;
139  }
140  }
141 
142  ring_read(&mx->index_extrbuffer[i], (uint8_t *)piu,
143  sizeof(index_unit));
144  length += piu->length;
145  piu = &niu;
146  }
147  if (j == 0)
148  return 0;
149  extiu->length = length;
150  extiu->framesize = length;
151  if(! peek_next_ext_unit(mx, &niu, i))
152  return 1;
153  //left-shift by 8 to increase precision
154  extiu->ptsrate = (uptsdiff(niu.pts, extiu->pts) << 8) / extiu->length;
155 
156 #ifdef OUT_DEBUG
157  LOG(VB_GENERAL, LOG_DEBUG,
158  QString("ext index start: %1 stop: %2 (%3) rpos: %4")
159  .arg(extiu->start).arg(extiu->start+extiu->length)
160  .arg(extiu->length).arg(ring_rpos(&mx->extrbuffer[i])));
161 #endif
162  return 1;
163 }
164 
165 static uint8_t get_ptsdts(multiplex_t *mx, index_unit *viu)
166 {
167  uint8_t ptsdts = 0;
168  switch (mx->frame_timestamps){
169  case TIME_ALWAYS:
170  if (viu->frame == I_FRAME || viu->frame == P_FRAME)
171  ptsdts = PTS_DTS;
172  else
173  ptsdts = PTS_ONLY;
174  break;
175 
176  case TIME_IFRAME:
177  if (viu->frame == I_FRAME)
178  ptsdts = PTS_DTS;
179  break;
180  }
181  return ptsdts;
182 }
183 
184 static void writeout_video(multiplex_t *mx)
185 {
186  uint8_t outbuf[3000];
187  int written=0;
188  uint8_t ptsdts=0;
189  int frame_len=0;
190  index_unit *viu = &mx->viu;
191 
192 #ifdef OUT_DEBUG
193  LOG(VB_GENERAL, LOG_DEBUG, "writing VIDEO pack");
194 #endif
195 
196  if(viu->frame_start) {
197  ptsdts = get_ptsdts(mx, viu);
198  frame_len = viu->length;
199  }
200 
201  if (viu->frame_start && viu->seq_header && viu->gop &&
202  viu->frame == I_FRAME){
203  if (!mx->startup && mx->is_ts){
204  write_ts_patpmt(mx->ext, mx->extcnt, 1, outbuf);
205  write(mx->fd_out, outbuf, mx->pack_size*2);
206  ptsinc(&mx->SCR, mx->SCRinc*2);
207  } else if (!mx->startup && mx->navpack){
208  write_nav_pack(mx->pack_size, mx->extcnt,
209  mx->SCR, mx->muxr, outbuf);
210  write(mx->fd_out, outbuf, mx->pack_size);
211  ptsinc(&mx->SCR, mx->SCRinc);
212  } else mx->startup = 0;
213 #ifdef OUT_DEBUG
214  LOG(VB_GENERAL, LOG_DEBUG, " with sequence and gop header");
215 #endif
216  }
217 
218  if (mx->finish != 2 && dummy_space(&mx->vdbuf) < mx->data_size){
219  return;
220  }
221  unsigned int length = viu->length;
222  while (!mx->is_ts && length < mx->data_size){
223  index_unit nviu {};
224  int old_start = viu->frame_start;
225  int old_frame = viu->frame;
226  uint64_t old_pts = viu->pts;
227  uint64_t old_dts = viu->dts;
228  dummy_add(&mx->vdbuf, uptsdiff(viu->dts+mx->video_delay,0)
229  , viu->length);
230  if ( peek_next_video_unit(mx, &nviu)){
231  if (!(nviu.seq_header && nviu.gop &&
232  nviu.frame == I_FRAME)){
233  get_next_video_unit(mx, viu);
234  frame_len = viu->length;
235  length += viu->length;
236  if(old_start) {
237  viu->pts = old_pts;
238  viu->dts = old_dts;
239  viu->frame = old_frame;
240  } else {
241  ptsdts = get_ptsdts(mx, viu);
242  }
243  } else break;
244  } else break;
245  }
246 
247  if (viu->frame_start){
248  viu->frame_start=0;
249  if (viu->gop){
250  uint8_t gop[8];
251  frame_len=length-frame_len;
252  ring_peek(mx->vrbuffer, gop, 8, frame_len);
253  pts2time( viu->pts + mx->video_delay, gop, 8);
254  ring_poke(mx->vrbuffer, gop, 8, frame_len);
255  viu->gop=0;
256  }
257  if (mx->VBR) {
258  mx->extra_clock = ptsdiff(viu->dts + mx->video_delay,
259  mx->SCR + 500*CLOCK_MS);
260 #ifdef OUT_DEBUG1
261  LOG(VB_GENERAL, LOG_DEBUG,
262  QString("EXTRACLOCK2: %1 %2 %3")
263  .arg(viu->dts).arg(mx->video_delay).arg(mx->SCR));
264  LOG(VB_GENERAL, LOG_DEBUG, QString("EXTRACLOCK2: %1")
265  .arg(mx->extra_clock));
266  printpts(mx->extra_clock);
267 #endif
268 
269  if (mx->extra_clock < 0)
270  mx->extra_clock = 0.0;
271  }
272  }
273 
274 
275  int nlength = length;
276  if (mx->is_ts) {
277  written = write_video_ts( viu->pts+mx->video_delay,
278  viu->dts+mx->video_delay,
279  mx->SCR, outbuf, &nlength,
280  ptsdts, mx->vrbuffer);
281  } else {
282  written = write_video_pes( mx->pack_size, mx->extcnt,
283  viu->pts+mx->video_delay,
284  viu->dts+mx->video_delay,
285  mx->SCR, mx->muxr, outbuf, &nlength,
286  ptsdts, mx->vrbuffer);
287  }
288 
289  // something bad happened with the PES or TS write, bail
290  if (written == -1)
291  return;
292 
293  length -= nlength;
294  dummy_add(&mx->vdbuf, uptsdiff( viu->dts+mx->video_delay,0)
295  , viu->length-length);
296  viu->length = length;
297 
298  //estimate next pts based on bitrate of this stream and data written
299  viu->dts = uptsdiff(viu->dts + ((nlength*viu->ptsrate)>>8), 0);
300 
301  write(mx->fd_out, outbuf, written);
302 
303 #ifdef OUT_DEBUG
304  LOG(VB_GENERAL, LOG_DEBUG, "VPTS");
305  printpts(viu->pts);
306  LOG(VB_GENERAL, LOG_DEBUG, " DTS");
307  printpts(viu->dts);
308  printpts(mx->video_delay);
309 #endif
310 
311  if (viu->length == 0){
312  get_next_video_unit(mx, viu);
313  }
314 
315 }
316 
317 static void writeout_ext(multiplex_t *mx, int n)
318 {
319  uint8_t outbuf[3000];
320  int written=0;
321  uint64_t dpts=0;
322  int newpts=0;
323  int nframes=1;
324  int ac3_off=0;
325  int rest_data = 5;
326 
327  int type = mx->ext[n].type;
328  ringbuffer *airbuffer = &mx->index_extrbuffer[n];
329  dummy_buffer *dbuf = &mx->ext[n].dbuf;
330  uint64_t adelay = mx->ext[n].pts_off;
331  uint64_t *apts = &mx->ext[n].pts;
332  index_unit *aiu = &mx->ext[n].iu;
333 
334  switch (type){
335 
336  case MPEG_AUDIO:
337 #ifdef OUT_DEBUG
338  LOG(VB_GENERAL, LOG_DEBUG, QString("writing AUDIO%1 pack\n").arg(n));
339 #endif
340  break;
341 
342  case AC3:
343 #ifdef OUT_DEBUG
344  LOG(VB_GENERAL, LOG_DEBUG, QString("writing AC3%1 pack\n").arg(n));
345 #endif
346  rest_data = 1; // 4 bytes AC3 header
347  break;
348 
349  default:
350  return;
351  }
352 
353  if (mx->finish != 2 && dummy_space(dbuf) < mx->data_size + rest_data){
354  return;
355  }
356 
357  uint64_t pts = uptsdiff( aiu->pts + mx->audio_delay, adelay );
358  *apts = pts;
359  unsigned int length = aiu->length;
360  if (length < aiu->framesize){
361  newpts = 1;
362  ac3_off = length;
363  }
364  dummy_add(dbuf, pts, aiu->length);
365 
366 #ifdef OUT_DEBUG
367  LOG(VB_GENERAL, LOG_DEBUG, QString("start: %1 stop: %2 (%3) length %4")
368  .arg(aiu->start).arg(aiu->start+aiu->length)
369  .arg(aiu->length).arg(length));
370  printpts(*apts);
371  printpts(aiu->pts);
372  printpts(mx->audio_delay);
373  printpts(adelay);
374  printpts(pts);
375 #endif
376  while (!mx->is_ts && length < mx->data_size + rest_data){
377  if (ring_read(airbuffer, (uint8_t *)aiu, sizeof(index_unit)) > 0){
378  dpts = uptsdiff(aiu->pts +mx->audio_delay, adelay );
379 
380  if (newpts){
381  pts = dpts;
382  newpts=0;
383  }
384 
385  length+= aiu->length;
386  if (length < mx->data_size + rest_data)
387  dummy_add(dbuf, dpts, aiu->length);
388 
389  *apts = dpts;
390  nframes++;
391 #ifdef OUT_DEBUG
392  LOG(VB_GENERAL, LOG_DEBUG,
393  QString("start: %1 stop: %2 (%3) length %4")
394  .arg(aiu->start).arg(aiu->start+aiu->length)
395  .arg(aiu->length).arg(length));
396  printpts(*apts);
397  printpts(aiu->pts);
398  printpts(mx->audio_delay);
399  printpts(adelay);
400 #endif
401  } else if (mx->finish){
402  break;
403  } else if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
404  LOG(VB_GENERAL, LOG_ERR, "error in writeout ext");
405  exit(1);
406  }
407  }
408 
409  int nlength = length;
410 
411  switch (type) {
412  case MPEG_AUDIO:
413  if(mx->is_ts) {
414  written = write_audio_ts( mx->ext[n].strmnum, pts,
415  outbuf, &nlength, newpts ? 0 : PTS_ONLY,
416  &mx->extrbuffer[n]);
417  } else {
418  written = write_audio_pes( mx->pack_size, mx->extcnt,
419  mx->ext[n].strmnum, pts, mx->SCR,
420  mx->muxr, outbuf, &nlength, PTS_ONLY,
421  &mx->extrbuffer[n]);
422  }
423  break;
424  case AC3:
425  if(mx->is_ts) {
426  written = write_ac3_ts(mx->ext[n].strmnum, pts,
427  outbuf, &nlength, newpts ? 0 : PTS_ONLY,
428  mx->ext[n].frmperpkt, &mx->extrbuffer[n]);
429  } else {
430  written = write_ac3_pes( mx->pack_size, mx->extcnt,
431  mx->ext[n].strmnum, pts, mx->SCR,
432  mx->muxr, outbuf, &nlength, PTS_ONLY,
433  nframes, ac3_off,
434  &mx->extrbuffer[n]);
435  }
436  break;
437  }
438 
439  // something bad happened when writing TS or PES to the MPEG or AC3
440  // audio stream
441  if (written == -1)
442  return;
443 
444  length -= nlength;
445  write(mx->fd_out, outbuf, written);
446 
447  dummy_add(dbuf, dpts, aiu->length-length);
448  aiu->length = length;
449  aiu->start = ring_rpos(&mx->extrbuffer[n]);
450 
451  if (aiu->length == 0){
452  get_next_ext_unit(mx, aiu, n);
453  } else {
454  //estimate next pts based on bitrate of stream and data written
455  aiu->pts = uptsdiff(aiu->pts + ((nlength*aiu->ptsrate)>>8), 0);
456  }
457  *apts = uptsdiff(aiu->pts + mx->audio_delay, adelay);
458 #ifdef OUT_DEBUG
459  if ((int64_t)*apts < 0)
460  LOG(VB_GENERAL, LOG_DEBUG, "SCHEISS APTS");
461  printpts(*apts);
462  printpts(aiu->pts);
463  printpts(mx->audio_delay);
464  printpts(adelay);
465 #endif
466 
467  if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
468  LOG(VB_GENERAL, LOG_ERR, "error in writeout ext");
469  exit(1);
470  }
471 }
472 
473 static void writeout_padding (multiplex_t *mx)
474 {
475  uint8_t outbuf[3000];
476 #if 0
477  LOG(VB_GENERAL, LOG_INFO, "writing PADDING pack");
478 #endif
479 
480  write_padding_pes( mx->pack_size, mx->extcnt, mx->SCR,
481  mx->muxr, outbuf);
482  write(mx->fd_out, outbuf, mx->pack_size);
483 }
484 
485 void check_times( multiplex_t *mx, int *video_ok, int *ext_ok, int *start)
486 {
487  int set_ok = 0;
488 
489  memset(ext_ok, 0, N_AUDIO*sizeof(int));
490  *video_ok = 0;
491 
492  if (mx->fill_buffers(mx->priv, mx->finish)< 0) {
493  LOG(VB_GENERAL, LOG_ERR, "error in get next video unit");
494  return;
495  }
496 
497  /* increase SCR to next packet */
498  mx->oldSCR = mx->SCR;
499  if (!*start){
500  ptsinc(&mx->SCR, mx->SCRinc);
501  } else *start = 0;
502 
503  if (mx->VBR) {
504 #ifdef OUT_DEBUG1
505  LOG(VB_GENERAL, LOG_DEBUG,
506  QString("EXTRACLOCK: %1").arg(mx->extra_clock));
507  printpts(mx->extra_clock);
508 #endif
509 
510  if (mx->extra_clock > 0.0) {
511  int64_t d = mx->extra_clock/ mx->SCRinc - 1;
512  if (d > 0)
513  mx->extra_clock = d*mx->SCRinc;
514  else
515  mx->extra_clock = 0.0;
516  }
517 
518  if (mx->extra_clock > 0.0) {
519  int64_t temp_scr = mx->extra_clock;
520 
521  for (int i=0; i<mx->extcnt; i++){
522  if (ptscmp(mx->SCR + temp_scr + 100*CLOCK_MS,
523  mx->ext[i].iu.pts) > 0) {
524  while (ptscmp(mx->SCR + temp_scr
525  + 100*CLOCK_MS,
526  mx->ext[i].iu.pts) > 0)
527  temp_scr -= mx->SCRinc;
528  temp_scr += mx->SCRinc;
529  }
530  }
531 
532  if (temp_scr > 0.0) {
533  mx->SCR += temp_scr;
534  mx->extra_clock -= temp_scr;
535  } else
536  mx->extra_clock = 0.0;
537  }
538  }
539 
540  /* clear decoder buffers up to SCR */
541  dummy_delete(&mx->vdbuf, mx->SCR);
542 
543  for (int i=0;i <mx->extcnt; i++)
544  dummy_delete(&mx->ext[i].dbuf, mx->SCR);
545 
546  if (dummy_space(&mx->vdbuf) > mx->vsize && mx->viu.length > 0 &&
547  (ptscmp(mx->viu.dts + mx->video_delay, 500*CLOCK_MS +mx->oldSCR)<0)
548  && ring_avail(mx->index_vrbuffer)){
549  *video_ok = 1;
550  set_ok = 1; //NOLINT(clang-analyzer-deadcode.DeadStores)
551  }
552 
553  for (int i = 0; i < mx->extcnt; i++){
554  if (dummy_space(&mx->ext[i].dbuf) > mx->extsize &&
555  mx->ext[i].iu.length > 0 &&
556  ptscmp(mx->ext[i].pts, 500*CLOCK_MS + mx->oldSCR) < 0
557  && ring_avail(&mx->index_extrbuffer[i])){
558  ext_ok[i] = 1;
559  set_ok = 1; //NOLINT(clang-analyzer-deadcode.DeadStores)
560  }
561  }
562 #ifdef OUT_DEBUG
563  if (set_ok) {
564  LOG(VB_GENERAL, LOG_DEBUG, "SCR");
565  printpts(mx->oldSCR);
566  LOG(VB_GENERAL, LOG_DEBUG, "VDTS");
567  printpts(mx->viu.dts);
568  LOG(VB_GENERAL, LOG_DEBUG, QString(" (%1) EXT").arg(*video_ok));
569  for (int i = 0; i < mx->extcnt; i++){
570  LOG(VB_GENERAL, LOG_DEBUG,
571  QString("%1:").arg(mx->ext[i].type));
572  printpts(mx->ext[i].pts);
573  LOG(VB_GENERAL, LOG_DEBUG,
574  QString(" (%1)").arg(ext_ok[i]));
575  }
576  }
577 #else
578  (void)set_ok;
579 #endif
580 }
581 void write_out_packs( multiplex_t *mx, int video_ok, int *ext_ok)
582 {
583  if (video_ok && use_video(mx->viu.dts + mx->video_delay,
584  mx->ext, ext_ok, mx->extcnt)) {
585  writeout_video(mx);
586  } else { // second case(s): audio ok, video in time
587  int i = which_ext(mx->ext, ext_ok, mx->extcnt);
588  int done=0;
589  if(i>=0) {
590  writeout_ext(mx, i);
591  done = 1;
592  }
593  if (!done && !mx->VBR){
594  writeout_padding(mx);
595  }
596  }
597 
598 }
599 
601 {
602  int start=0;
603  int video_ok = 0;
604  int ext_ok[N_AUDIO];
605  int n = 0;
606  uint8_t mpeg_end[4] = { 0x00, 0x00, 0x01, 0xB9 };
607 
608  memset(ext_ok, 0, N_AUDIO*sizeof(int));
609  mx->finish = 1;
610 
611  int old = 0;
612  int nn=0;
613  while ((n=buffers_filled(mx)) && nn<1000 ){
614  if (n== old) nn++;
615  else nn=0;
616  old = n;
617  check_times( mx, &video_ok, ext_ok, &start);
618  write_out_packs( mx, video_ok, ext_ok);
619  }
620 
621  old = 0;nn=0;
622  while ((n=ring_avail(mx->index_vrbuffer)/sizeof(index_unit))
623  && nn<1000){
624  if (n== old) nn++;
625  else nn= 0;
626  old = n;
627  writeout_video(mx);
628  }
629 
630 // flush the rest
631  mx->finish = 2;
632  old = 0;nn=0;
633  for (int i = 0; i < mx->extcnt; i++){
634  while ((n=ring_avail(&mx->index_extrbuffer[i])/
635  sizeof(index_unit)) && nn <100){
636  if (n== old) nn++;
637  else nn = 0;
638  old = n;
639  writeout_ext(mx, i);
640  }
641  }
642 
643  if (mx->otype == REPLEX_MPEG2)
644  write(mx->fd_out, mpeg_end,4);
645 
646  dummy_destroy(&mx->vdbuf);
647  for (int i=0; i<mx->extcnt;i++)
648  dummy_destroy(&mx->ext[i].dbuf);
649 }
650 
651 static int get_ts_video_overhead(int pktsize, sequence_t *seq)
652 {
653  int pktdata = pktsize - TS_HEADER_MIN;
654  uint32_t framesize = seq->bit_rate * 50 / seq->frame_rate; //avg bytes/frame
655  uint32_t numpkt = (framesize + PES_H_MIN + 10 + pktdata -1) / pktdata;
656  return pktsize- ((pktsize * numpkt) - framesize + numpkt - 1) / numpkt;
657 }
658 
659 static int get_ts_ext_overhead(int pktsize, audio_frame_t *extframe,
660  extdata_t *ext, int cnt)
661 {
662  int max = 0;
663  int pktdata = pktsize - TS_HEADER_MIN;
664  for (int i = 0; i < cnt; i++) {
665  // 1/53 is approx 0.15 * 1/8 which allows us to calculate the
666  // # of packets in .15 seconds (which is the number of packets
667  // per PES.
668  ext[i].frmperpkt = extframe[i].bit_rate / 53 /
669  extframe[i].framesize;
670  int size = extframe[i].framesize * ext[i].frmperpkt;
671  int numpkt = (size + pktdata - 1) / pktdata;
672  int overhead = (pktsize * numpkt - size + numpkt - 1) / numpkt;
673  if(overhead > max)
674  max = overhead;
675  }
676  return pktsize - max;
677 }
678 
679 void init_multiplex( multiplex_t *mx, sequence_t *seq_head,
680  audio_frame_t *extframe, int *exttype, const int *exttypcnt,
681  uint64_t video_delay, uint64_t audio_delay, int fd,
682  int (*fill_buffers)(void *p, int f),
683  ringbuffer *vrbuffer, ringbuffer *index_vrbuffer,
684  ringbuffer *extrbuffer, ringbuffer *index_extrbuffer,
685  int otype)
686 {
687  int i = 0;
688  uint32_t data_rate = 0;
689 
691  mx->video_delay = video_delay;
692  mx->audio_delay = audio_delay;
693  mx->fd_out = fd;
694  mx->otype = otype;
695 
696  switch(mx->otype){
697 
698  case REPLEX_DVD:
699  mx->video_delay += 180*CLOCK_MS;
700  mx->audio_delay += 180*CLOCK_MS;
701  mx->pack_size = 2048;
702  mx->audio_buffer_size = 4*1024;
703  mx->video_buffer_size = 232*1024;
704  mx->mux_rate = 1260000;
705  mx->navpack = 1;
707  mx->VBR = 1;
708  mx->reset_clocks = 0;
709  mx->write_end_codes = 0;
710  mx->set_broken_link = 0;
711  mx->is_ts = 0;
712  break;
713 
714 
715  case REPLEX_MPEG2:
716  mx->video_delay += 180*CLOCK_MS;
717  mx->audio_delay += 180*CLOCK_MS;
718  mx->pack_size = 2048;
719  mx->audio_buffer_size = 4*1024;
720  mx->video_buffer_size = 224*1024;
721  mx->mux_rate = 0;
722  mx->navpack = 0;
724  mx->VBR = 1;
725  mx->reset_clocks = 1;
726  mx->write_end_codes = 1;
727  mx->set_broken_link = 1;
728  mx->is_ts = 0;
729  break;
730 
731  case REPLEX_HDTV:
732  mx->video_delay += 180*CLOCK_MS;
733  mx->audio_delay += 180*CLOCK_MS;
734  mx->pack_size = 2048;
735  mx->audio_buffer_size = 4*1024;
736  mx->video_buffer_size = 4*224*1024;
737  mx->mux_rate = 0;
738  mx->navpack = 0;
740  mx->VBR = 1;
741  mx->reset_clocks = 1;
742  mx->write_end_codes = 1;
743  mx->set_broken_link = 1;
744  mx->is_ts = 0;
745  break;
746 
747  case REPLEX_TS_SD:
748  mx->video_delay += 180*CLOCK_MS;
749  mx->audio_delay += 180*CLOCK_MS;
750  mx->pack_size = 188;
751  mx->audio_buffer_size = 4*1024;
752  mx->video_buffer_size = 232*1024;
753  mx->mux_rate = 1260000;
754  mx->navpack = 0;
756  mx->VBR = 1;
757  mx->reset_clocks = 0;
758  mx->write_end_codes = 0;
759  mx->set_broken_link = 0;
760  mx->is_ts = 1;
761  break;
762 
763  case REPLEX_TS_HD:
764  mx->video_delay += 180*CLOCK_MS;
765  mx->audio_delay += 180*CLOCK_MS;
766  mx->pack_size = 188;
767  mx->audio_buffer_size = 4*1024;
768  mx->video_buffer_size = 4*224*1024;
769  mx->mux_rate = 0;
770  mx->navpack = 0;
772  mx->VBR = 1;
773  mx->reset_clocks = 0;
774  mx->write_end_codes = 0;
775  mx->set_broken_link = 0;
776  mx->is_ts = 1;
777  break;
778  }
779 
780  for (mx->extcnt = 0, data_rate = 0, i = 0;
781  i < N_AUDIO && exttype[i]; i++){
782  if (exttype[i] >= MAX_TYPES) {
783  LOG(VB_GENERAL, LOG_ERR,
784  QString("Found illegal stream type %1").arg(exttype[i]));
785  exit(1);
786  }
787  mx->ext[i].type = exttype[i];
788  mx->ext[i].pts_off = 0;
789  mx->ext[i].frmperpkt = 1;
790  mx->ext[i].strmnum = exttypcnt[i];
791  strncpy(mx->ext[i].language, extframe[i].language, 4);
792  dummy_init(&mx->ext[i].dbuf, mx->audio_buffer_size);
793  data_rate += extframe[i].bit_rate;
794  mx->extcnt++;
795  }
796 
797  mx->vrbuffer = vrbuffer;
798  mx->index_vrbuffer = index_vrbuffer;
799  mx->extrbuffer = extrbuffer;
800  mx->index_extrbuffer = index_extrbuffer;
801 
803 
804  //NOTE: vpkt_hdr/extpkt_hdr are worst-case for PS streams, but
805  //best-guess for TS streams
806  if(mx->is_ts) {
807  //Use best guess for TS streams
808  mx->data_size = get_ts_video_overhead(mx->pack_size, seq_head);
809  mx->extsize = get_ts_ext_overhead(mx->pack_size, extframe,
810  mx->ext, mx->extcnt);
811 
812  } else {
813  //Use worst case for PS streams
814  mx->data_size = mx->pack_size - PES_H_MIN - PS_HEADER_L1 - 10;
815  mx->extsize = mx->data_size + 5; //one less DTS
816  }
817  mx->vsize = mx->data_size;
818 
819  data_rate += seq_head->bit_rate *400;
820 
821  mx->muxr = ((uint64_t)data_rate / 8 * mx->pack_size) / mx->data_size;
822  // muxrate of payload in Byte/s
823 
824  if (mx->mux_rate) {
825  if ( mx->mux_rate < mx->muxr)
826  LOG(VB_GENERAL, LOG_WARNING,
827  "data rate may be to high for required mux rate");
828  mx->muxr = mx->mux_rate;
829  }
830  LOG(VB_GENERAL, LOG_INFO, QString("Mux rate: %1 Mbit/s")
831  .arg(mx->muxr*8.0/1000000.0, 0,'f',2,QChar('0')));
832 
833  mx->SCRinc = 27000000ULL/((uint64_t)mx->muxr /
834  (uint64_t) mx->pack_size);
835 
836 }
837 
839 {
840  get_next_video_unit(mx, &mx->viu);
841  for (int i=0; i < mx->extcnt; i++)
842  {
843  get_next_ext_unit(mx, &mx->ext[i].iu, i);
844  if (mx->ext[i].type == MPEG_AUDIO || mx->ext[i].type == AC3) {
845  mx->ext[i].pts = uptsdiff(
846  mx->ext[i].iu.pts + mx->audio_delay,
847  mx->ext[i].pts_off);
848  } else {
849  mx->ext[i].pts = uptsdiff(
850  mx->ext[i].iu.pts, mx->ext[i].pts_off);
851  }
852  }
853 
854  mx->SCR = 0;
855 
856  // write first VOBU header
857  if (mx->is_ts) {
858  uint8_t outbuf[2048];
859  write_ts_patpmt(mx->ext, mx->extcnt, 1, outbuf);
860  write(mx->fd_out, outbuf, mx->pack_size*2);
861  ptsinc(&mx->SCR, mx->SCRinc*2);
862  mx->startup = 1;
863  } else if (mx->navpack){
864  uint8_t outbuf[2048];
865  write_nav_pack(mx->pack_size, mx->extcnt,
866  mx->SCR, mx->muxr, outbuf);
867  write(mx->fd_out, outbuf, mx->pack_size);
868  ptsinc(&mx->SCR, mx->SCRinc);
869  mx->startup = 1;
870  } else mx->startup = 0;
871 }
extdata_t::pts_off
uint64_t pts_off
Definition: mpg_common.h:57
extdata_t::type
int type
Definition: mpg_common.h:58
pts2time
void pts2time(uint64_t pts, uint8_t *buf, int len)
Definition: element.cpp:154
index_unit::ptsrate
uint64_t ptsrate
Definition: mpg_common.h:51
get_ts_video_overhead
static int get_ts_video_overhead(int pktsize, sequence_t *seq)
Definition: multiplex.cpp:651
get_next_ext_unit
static int get_next_ext_unit(multiplex_t *mx, index_unit *extiu, int i)
Definition: multiplex.cpp:124
index_unit::frame_start
uint8_t frame_start
Definition: mpg_common.h:48
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:292
peek_next_video_unit
static int peek_next_video_unit(multiplex_t *mx, index_unit *viu)
Definition: multiplex.cpp:50
dummy_init
int dummy_init(dummy_buffer *dbuf, int s)
Definition: ringbuffer.cpp:388
CLOCK_MS
#define CLOCK_MS
Definition: element.h:77
sequence_t::frame_rate
uint32_t frame_rate
Definition: element.h:92
get_ptsdts
static uint8_t get_ptsdts(multiplex_t *mx, index_unit *viu)
Definition: multiplex.cpp:165
multiplex_t::finish
int finish
Definition: multiplex.h:45
fill_buffers
static int fill_buffers(void *r, int finish)
Definition: replex.cpp:1836
multiplex_t::oldSCR
uint64_t oldSCR
Definition: multiplex.h:68
ring_peek
int ring_peek(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:122
dummy_delete
int dummy_delete(dummy_buffer *dbuf, uint64_t time)
Definition: ringbuffer.cpp:431
ringbuffer
Definition: ringbuffer.h:36
extdata_t::strmnum
int strmnum
Definition: mpg_common.h:59
multiplex_t::audio_buffer_size
uint32_t audio_buffer_size
Definition: multiplex.h:52
multiplex_t::vrbuffer
ringbuffer * vrbuffer
Definition: multiplex.h:79
sequence_t
Definition: element.h:86
TS_HEADER_MIN
#define TS_HEADER_MIN
Definition: pes.h:12
MAX_TYPES
@ MAX_TYPES
Definition: element.h:83
audio_frame_t::language
char language[4]
Definition: element.h:119
TIME_ALWAYS
#define TIME_ALWAYS
Definition: multiplex.h:57
d
static const uint16_t * d
Definition: iso6937tables.cpp:1025
index_unit::dts
uint64_t dts
Definition: mpg_common.h:40
writeout_video
static void writeout_video(multiplex_t *mx)
Definition: multiplex.cpp:184
multiplex_t::mux_rate
uint32_t mux_rate
Definition: multiplex.h:54
multiplex_t::fd_out
int fd_out
Definition: multiplex.h:37
mythburn.write
def write(text, progress=True)
Definition: mythburn.py:308
AC3
@ AC3
Definition: element.h:83
audio_frame_t::bit_rate
uint32_t bit_rate
Definition: element.h:111
arg
arg(title).arg(filename).arg(doDelete))
multiplex_t::viu
index_unit viu
Definition: multiplex.h:70
write_ts_patpmt
void write_ts_patpmt(extdata_t *ext, int extcnt, uint8_t prog_num, uint8_t *buf)
Definition: ts.cpp:380
extdata_t::language
char language[4]
Definition: mpg_common.h:61
REPLEX_TS_HD
#define REPLEX_TS_HD
Definition: multiplex.h:42
extdata_t::iu
index_unit iu
Definition: mpg_common.h:55
write_video_pes
int write_video_pes(int pack_size, int extcnt, uint64_t vpts, uint64_t vdts, uint64_t SCR, uint64_t muxr, uint8_t *buf, int *vlength, uint8_t ptsdts, ringbuffer *vrbuffer)
Definition: pes.cpp:687
ptsinc
static void ptsinc(uint64_t *pts1, uint64_t pts2)
Definition: pes.h:127
finish_mpg
void finish_mpg(multiplex_t *mx)
Definition: multiplex.cpp:600
printpts
void printpts(int64_t pts)
Definition: pes.cpp:42
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
init_multiplex
void init_multiplex(multiplex_t *mx, sequence_t *seq_head, audio_frame_t *extframe, int *exttype, const int *exttypcnt, uint64_t video_delay, uint64_t audio_delay, int fd, int(*fill_buffers)(void *p, int f), ringbuffer *vrbuffer, ringbuffer *index_vrbuffer, ringbuffer *extrbuffer, ringbuffer *index_extrbuffer, int otype)
Definition: multiplex.cpp:679
sequence_t::bit_rate
uint32_t bit_rate
Definition: element.h:93
REPLEX_HDTV
#define REPLEX_HDTV
Definition: multiplex.h:40
I_FRAME
#define I_FRAME
Definition: element.h:53
multiplex_t::navpack
uint8_t navpack
Definition: multiplex.h:56
multiplex_t::otype
int otype
Definition: multiplex.h:43
index_unit::seq_header
uint8_t seq_header
Definition: mpg_common.h:41
P_FRAME
#define P_FRAME
Definition: element.h:54
ptsdiff
int64_t ptsdiff(uint64_t pts1, uint64_t pts2)
Definition: pes.cpp:77
writeout_ext
static void writeout_ext(multiplex_t *mx, int n)
Definition: multiplex.cpp:317
write_out_packs
void write_out_packs(multiplex_t *mx, int video_ok, int *ext_ok)
Definition: multiplex.cpp:581
PES_H_MIN
#define PES_H_MIN
Definition: pes.h:16
write_ac3_pes
int write_ac3_pes(int pack_size, int extcnt, int n, uint64_t pts, uint64_t SCR, uint32_t muxr, uint8_t *buf, int *alength, uint8_t ptsdts, int nframes, int ac3_off, ringbuffer *ac3rbuffer)
Definition: pes.cpp:782
multiplex_t::index_extrbuffer
ringbuffer * index_extrbuffer
Definition: multiplex.h:78
mythlogging.h
multiplex_t::write_end_codes
int write_end_codes
Definition: multiplex.h:63
multiplex_t::vsize
unsigned int vsize
Definition: multiplex.h:65
PS_HEADER_L1
#define PS_HEADER_L1
Definition: pes.h:13
hardwareprofile.config.p
p
Definition: config.py:33
index_unit::start
uint32_t start
Definition: mpg_common.h:38
PTS_ONLY
#define PTS_ONLY
Definition: pes.h:46
peek_next_ext_unit
static int peek_next_ext_unit(multiplex_t *mx, index_unit *extiu, int i)
Definition: multiplex.cpp:100
dummy_destroy
void dummy_destroy(dummy_buffer *dbuf)
Definition: ringbuffer.cpp:400
N_AUDIO
#define N_AUDIO
Definition: multiplex.h:33
multiplex_t::index_vrbuffer
ringbuffer * index_vrbuffer
Definition: multiplex.h:80
which_ext
static int which_ext(extdata_t *ext, const int *aok, int n)
Definition: multiplex.cpp:30
REPLEX_DVD
#define REPLEX_DVD
Definition: multiplex.h:39
dummy_add
int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size)
Definition: ringbuffer.cpp:413
multiplex_t::audio_delay
uint64_t audio_delay
Definition: multiplex.h:49
extdata_t
Definition: mpg_common.h:54
multiplex_t::VBR
int VBR
Definition: multiplex.h:60
f
QTextStream t & f
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:600
multiplex_t::set_broken_link
int set_broken_link
Definition: multiplex.h:64
index_unit::frame
uint8_t frame
Definition: mpg_common.h:45
multiplex_t::SCR
uint64_t SCR
Definition: multiplex.h:67
extdata_t::frmperpkt
int frmperpkt
Definition: mpg_common.h:60
get_next_video_unit
static int get_next_video_unit(multiplex_t *mx, index_unit *viu)
Definition: multiplex.cpp:73
MPEG_AUDIO
@ MPEG_AUDIO
Definition: element.h:83
multiplex_t::vdbuf
dummy_buffer vdbuf
Definition: multiplex.h:72
multiplex_t::reset_clocks
int reset_clocks
Definition: multiplex.h:62
ring_rpos
static int ring_rpos(ringbuffer *rbuf)
Definition: ringbuffer.h:73
multiplex_t::pack_size
int pack_size
Definition: multiplex.h:50
writeout_padding
static void writeout_padding(multiplex_t *mx)
Definition: multiplex.cpp:473
index_unit::framesize
uint32_t framesize
Definition: mpg_common.h:50
use_video
static int use_video(uint64_t vpts, extdata_t *ext, const int *aok, int n)
Definition: multiplex.cpp:23
multiplex_t::extra_clock
int64_t extra_clock
Definition: multiplex.h:66
multiplex.h
index_unit::gop
uint8_t gop
Definition: mpg_common.h:43
uptsdiff
uint64_t uptsdiff(uint64_t pts1, uint64_t pts2)
Definition: pes.cpp:103
ring_read
int ring_read(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.cpp:180
dummy_buffer
Definition: ringbuffer.h:46
REPLEX_MPEG2
#define REPLEX_MPEG2
Definition: multiplex.h:38
write_padding_pes
void write_padding_pes(int pack_size, int extcnt, uint64_t SCR, uint64_t muxr, uint8_t *buf)
Definition: pes.cpp:675
REPLEX_TS_SD
#define REPLEX_TS_SD
Definition: multiplex.h:41
write_nav_pack
int write_nav_pack(int pack_size, int extcnt, uint64_t SCR, uint32_t muxr, uint8_t *buf)
Definition: pes.cpp:835
multiplex_t::extcnt
int extcnt
Definition: multiplex.h:75
ring_avail
static unsigned int ring_avail(ringbuffer *rbuf)
Definition: ringbuffer.h:99
multiplex_t::fill_buffers
int(* fill_buffers)(void *p, int f)
Definition: multiplex.h:82
write_audio_ts
int write_audio_ts(int n, uint64_t pts, uint8_t *buf, int *alength, uint8_t ptsdts, ringbuffer *arbuffer)
Definition: ts.cpp:287
multiplex_t::startup
int startup
Definition: multiplex.h:44
multiplex_t::muxr
uint32_t muxr
Definition: multiplex.h:55
multiplex_t::data_size
unsigned int data_size
Definition: multiplex.h:51
MThread::exit
void exit(int retcode=0)
Use this to exit from the thread if you are using a Qt event loop.
Definition: mthread.cpp:287
dummy_space
static uint32_t dummy_space(dummy_buffer *dbuf)
Definition: ringbuffer.h:108
multiplex_t::extsize
unsigned int extsize
Definition: multiplex.h:65
extdata_t::dbuf
dummy_buffer dbuf
Definition: mpg_common.h:62
get_ts_ext_overhead
static int get_ts_ext_overhead(int pktsize, audio_frame_t *extframe, extdata_t *ext, int cnt)
Definition: multiplex.cpp:659
setup_multiplex
void setup_multiplex(multiplex_t *mx)
Definition: multiplex.cpp:838
multiplex_t::video_buffer_size
uint32_t video_buffer_size
Definition: multiplex.h:53
write_audio_pes
int write_audio_pes(int pack_size, int extcnt, int n, uint64_t pts, uint64_t SCR, uint32_t muxr, uint8_t *buf, int *alength, uint8_t ptsdts, ringbuffer *arbuffer)
Definition: pes.cpp:737
audio_frame_t::framesize
uint32_t framesize
Definition: element.h:116
ptscmp
int ptscmp(uint64_t pts1, uint64_t pts2)
Definition: pes.cpp:113
extdata_t::pts
uint64_t pts
Definition: mpg_common.h:56
audio_frame_t
Definition: element.h:108
ring_poke
int ring_poke(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:151
multiplex_t
Definition: multiplex.h:36
write_video_ts
int write_video_ts(uint64_t vpts, uint64_t vdts, uint64_t SCR, uint8_t *buf, int *vlength, uint8_t ptsdts, ringbuffer *vrbuffer)
Definition: ts.cpp:232
check_times
void check_times(multiplex_t *mx, int *video_ok, int *ext_ok, int *start)
Definition: multiplex.cpp:485
multiplex_t::ext
extdata_t ext[N_AUDIO]
Definition: multiplex.h:74
PTS_DTS
#define PTS_DTS
Definition: pes.h:47
multiplex_t::priv
void * priv
Definition: multiplex.h:83
index_unit
Definition: mpg_common.h:35
multiplex_t::is_ts
int is_ts
Definition: multiplex.h:61
multiplex_t::video_delay
uint64_t video_delay
Definition: multiplex.h:48
multiplex_t::frame_timestamps
int frame_timestamps
Definition: multiplex.h:59
multiplex_t::extrbuffer
ringbuffer * extrbuffer
Definition: multiplex.h:77
write_ac3_ts
int write_ac3_ts(int n, uint64_t pts, uint8_t *buf, int *alength, uint8_t ptsdts, int nframes, ringbuffer *ac3rbuffer)
Definition: ts.cpp:330
buffers_filled
static int buffers_filled(multiplex_t *mx)
Definition: multiplex.cpp:9
ts.h
index_unit::pts
uint64_t pts
Definition: mpg_common.h:39
multiplex_t::SCRinc
uint64_t SCRinc
Definition: multiplex.h:69
index_unit::length
uint32_t length
Definition: mpg_common.h:37
TIME_IFRAME
#define TIME_IFRAME
Definition: multiplex.h:58