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, ext_arr &ext, const aok_arr &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(ext_arr &ext, const aok_arr &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  std::array<uint8_t,3000> outbuf {};
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.data(), mx->extcnt, 1, outbuf.data());
205  write(mx->fd_out, outbuf.data(), 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.data());
210  write(mx->fd_out, outbuf.data(), 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  std::vector<uint8_t> gop(8);
251  frame_len=length-frame_len;
252  ring_peek(mx->vrbuffer, gop, frame_len);
253  pts2time( viu->pts + mx->video_delay, gop.data(), 8);
254  ring_poke(mx->vrbuffer, gop.data(), 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.data(), &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.data(), &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.data(), 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  std::array<uint8_t,3000> outbuf {};
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.data(), &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.data(), &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.data(), &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.data(), &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.data(), 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  std::array<uint8_t,3000> outbuf {};
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.data());
482  write(mx->fd_out, outbuf.data(), mx->pack_size);
483 }
484 
485 void check_times( multiplex_t *mx, int *video_ok, aok_arr &ext_ok, int *start)
486 {
487  int set_ok = 0;
488 
489  ext_ok.fill(false);
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] = true;
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, aok_arr &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  aok_arr ext_ok {};
605  int n = 0;
606  std::array<uint8_t,4> mpeg_end { 0x00, 0x00, 0x01, 0xB9 };
607 
608  mx->finish = 1;
609 
610  int old = 0;
611  int nn=0;
612  while ((n=buffers_filled(mx)) && nn<1000 ){
613  if (n== old) nn++;
614  else nn=0;
615  old = n;
616  check_times( mx, &video_ok, ext_ok, &start);
617  write_out_packs( mx, video_ok, ext_ok);
618  }
619 
620  old = 0;nn=0;
621  while ((n=ring_avail(mx->index_vrbuffer)/sizeof(index_unit))
622  && nn<1000){
623  if (n== old) nn++;
624  else nn= 0;
625  old = n;
626  writeout_video(mx);
627  }
628 
629 // flush the rest
630  mx->finish = 2;
631  old = 0;nn=0;
632  for (int i = 0; i < mx->extcnt; i++){
633  while ((n=ring_avail(&mx->index_extrbuffer[i])/
634  sizeof(index_unit)) && nn <100){
635  if (n== old) nn++;
636  else nn = 0;
637  old = n;
638  writeout_ext(mx, i);
639  }
640  }
641 
642  if (mx->otype == REPLEX_MPEG2)
643  write(mx->fd_out, mpeg_end.data(), 4);
644 
645  dummy_destroy(&mx->vdbuf);
646  for (int i=0; i<mx->extcnt;i++)
647  dummy_destroy(&mx->ext[i].dbuf);
648 }
649 
650 static int get_ts_video_overhead(int pktsize, sequence_t *seq)
651 {
652  int pktdata = pktsize - TS_HEADER_MIN;
653  uint32_t framesize = seq->bit_rate * 50 / seq->frame_rate; //avg bytes/frame
654  uint32_t numpkt = (framesize + PES_H_MIN + 10 + pktdata -1) / pktdata;
655  return pktsize- ((pktsize * numpkt) - framesize + numpkt - 1) / numpkt;
656 }
657 
658 static int get_ts_ext_overhead(int pktsize, audio_frame_t *extframe,
659  extdata_t *ext, int cnt)
660 {
661  int max = 0;
662  int pktdata = pktsize - TS_HEADER_MIN;
663  for (int i = 0; i < cnt; i++) {
664  // 1/53 is approx 0.15 * 1/8 which allows us to calculate the
665  // # of packets in .15 seconds (which is the number of packets
666  // per PES.
667  ext[i].frmperpkt = extframe[i].bit_rate / 53 /
668  extframe[i].framesize;
669  int size = extframe[i].framesize * ext[i].frmperpkt;
670  int numpkt = (size + pktdata - 1) / pktdata;
671  int overhead = (pktsize * numpkt - size + numpkt - 1) / numpkt;
672  if(overhead > max)
673  max = overhead;
674  }
675  return pktsize - max;
676 }
677 
678 void init_multiplex( multiplex_t *mx, sequence_t *seq_head,
679  audio_frame_t *extframe, int *exttype, const int *exttypcnt,
680  uint64_t video_delay, uint64_t audio_delay, int fd,
681  int (*fill_buffers)(void *p, int f),
682  ringbuffer *vrbuffer, ringbuffer *index_vrbuffer,
683  ringbuffer *extrbuffer, ringbuffer *index_extrbuffer,
684  int otype)
685 {
686  int i = 0;
687  uint32_t data_rate = 0;
688 
690  mx->video_delay = video_delay;
691  mx->audio_delay = audio_delay;
692  mx->fd_out = fd;
693  mx->otype = otype;
694 
695  switch(mx->otype){
696 
697  case REPLEX_DVD:
698  mx->video_delay += 180*CLOCK_MS;
699  mx->audio_delay += 180*CLOCK_MS;
700  mx->pack_size = 2048;
701  mx->audio_buffer_size = 4*1024;
702  mx->video_buffer_size = 232*1024;
703  mx->mux_rate = 1260000;
704  mx->navpack = 1;
706  mx->VBR = 1;
707  mx->reset_clocks = 0;
708  mx->write_end_codes = 0;
709  mx->set_broken_link = 0;
710  mx->is_ts = 0;
711  break;
712 
713 
714  case REPLEX_MPEG2:
715  mx->video_delay += 180*CLOCK_MS;
716  mx->audio_delay += 180*CLOCK_MS;
717  mx->pack_size = 2048;
718  mx->audio_buffer_size = 4*1024;
719  mx->video_buffer_size = 224*1024;
720  mx->mux_rate = 0;
721  mx->navpack = 0;
723  mx->VBR = 1;
724  mx->reset_clocks = 1;
725  mx->write_end_codes = 1;
726  mx->set_broken_link = 1;
727  mx->is_ts = 0;
728  break;
729 
730  case REPLEX_HDTV:
731  mx->video_delay += 180*CLOCK_MS;
732  mx->audio_delay += 180*CLOCK_MS;
733  mx->pack_size = 2048;
734  mx->audio_buffer_size = 4*1024;
735  mx->video_buffer_size = 4*224*1024;
736  mx->mux_rate = 0;
737  mx->navpack = 0;
739  mx->VBR = 1;
740  mx->reset_clocks = 1;
741  mx->write_end_codes = 1;
742  mx->set_broken_link = 1;
743  mx->is_ts = 0;
744  break;
745 
746  case REPLEX_TS_SD:
747  mx->video_delay += 180*CLOCK_MS;
748  mx->audio_delay += 180*CLOCK_MS;
749  mx->pack_size = 188;
750  mx->audio_buffer_size = 4*1024;
751  mx->video_buffer_size = 232*1024;
752  mx->mux_rate = 1260000;
753  mx->navpack = 0;
755  mx->VBR = 1;
756  mx->reset_clocks = 0;
757  mx->write_end_codes = 0;
758  mx->set_broken_link = 0;
759  mx->is_ts = 1;
760  break;
761 
762  case REPLEX_TS_HD:
763  mx->video_delay += 180*CLOCK_MS;
764  mx->audio_delay += 180*CLOCK_MS;
765  mx->pack_size = 188;
766  mx->audio_buffer_size = 4*1024;
767  mx->video_buffer_size = 4*224*1024;
768  mx->mux_rate = 0;
769  mx->navpack = 0;
771  mx->VBR = 1;
772  mx->reset_clocks = 0;
773  mx->write_end_codes = 0;
774  mx->set_broken_link = 0;
775  mx->is_ts = 1;
776  break;
777  }
778 
779  for (mx->extcnt = 0, data_rate = 0, i = 0;
780  i < N_AUDIO && exttype[i]; i++){
781  if (exttype[i] >= MAX_TYPES) {
782  LOG(VB_GENERAL, LOG_ERR,
783  QString("Found illegal stream type %1").arg(exttype[i]));
784  exit(1);
785  }
786  mx->ext[i].type = exttype[i];
787  mx->ext[i].pts_off = 0;
788  mx->ext[i].frmperpkt = 1;
789  mx->ext[i].strmnum = exttypcnt[i];
790  strncpy(mx->ext[i].language, extframe[i].language, 4);
791  dummy_init(&mx->ext[i].dbuf, mx->audio_buffer_size);
792  data_rate += extframe[i].bit_rate;
793  mx->extcnt++;
794  }
795 
796  mx->vrbuffer = vrbuffer;
797  mx->index_vrbuffer = index_vrbuffer;
798  mx->extrbuffer = extrbuffer;
799  mx->index_extrbuffer = index_extrbuffer;
800 
802 
803  //NOTE: vpkt_hdr/extpkt_hdr are worst-case for PS streams, but
804  //best-guess for TS streams
805  if(mx->is_ts) {
806  //Use best guess for TS streams
807  mx->data_size = get_ts_video_overhead(mx->pack_size, seq_head);
808  mx->extsize = get_ts_ext_overhead(mx->pack_size, extframe,
809  mx->ext.data(), mx->extcnt);
810 
811  } else {
812  //Use worst case for PS streams
813  mx->data_size = mx->pack_size - PES_H_MIN - PS_HEADER_L1 - 10;
814  mx->extsize = mx->data_size + 5; //one less DTS
815  }
816  mx->vsize = mx->data_size;
817 
818  data_rate += seq_head->bit_rate *400;
819 
820  mx->muxr = ((uint64_t)data_rate / 8 * mx->pack_size) / mx->data_size;
821  // muxrate of payload in Byte/s
822 
823  if (mx->mux_rate) {
824  if ( mx->mux_rate < mx->muxr)
825  LOG(VB_GENERAL, LOG_WARNING,
826  "data rate may be to high for required mux rate");
827  mx->muxr = mx->mux_rate;
828  }
829  LOG(VB_GENERAL, LOG_INFO, QString("Mux rate: %1 Mbit/s")
830  .arg(mx->muxr*8.0/1000000.0, 0,'f',2,QChar('0')));
831 
832  mx->SCRinc = 27000000ULL/((uint64_t)mx->muxr /
833  (uint64_t) mx->pack_size);
834 
835 }
836 
838 {
839  get_next_video_unit(mx, &mx->viu);
840  for (int i=0; i < mx->extcnt; i++)
841  {
842  get_next_ext_unit(mx, &mx->ext[i].iu, i);
843  if (mx->ext[i].type == MPEG_AUDIO || mx->ext[i].type == AC3) {
844  mx->ext[i].pts = uptsdiff(
845  mx->ext[i].iu.pts + mx->audio_delay,
846  mx->ext[i].pts_off);
847  } else {
848  mx->ext[i].pts = uptsdiff(
849  mx->ext[i].iu.pts, mx->ext[i].pts_off);
850  }
851  }
852 
853  mx->SCR = 0;
854 
855  // write first VOBU header
856  if (mx->is_ts) {
857  std::array<uint8_t,2048> outbuf {};
858  write_ts_patpmt(mx->ext.data(), mx->extcnt, 1, outbuf.data());
859  write(mx->fd_out, outbuf.data(), mx->pack_size*2);
860  ptsinc(&mx->SCR, mx->SCRinc*2);
861  mx->startup = 1;
862  } else if (mx->navpack){
863  std::array<uint8_t,2048> outbuf {};
864  write_nav_pack(mx->pack_size, mx->extcnt,
865  mx->SCR, mx->muxr, outbuf.data());
866  write(mx->fd_out, outbuf.data(), mx->pack_size);
867  ptsinc(&mx->SCR, mx->SCRinc);
868  mx->startup = 1;
869  } else mx->startup = 0;
870 }
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:650
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:288
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:399
CLOCK_MS
#define CLOCK_MS
Definition: element.h:78
sequence_t::frame_rate
uint32_t frame_rate
Definition: element.h:93
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:50
fill_buffers
static int fill_buffers(void *r, int finish)
Definition: replex.cpp:1835
multiplex_t::oldSCR
uint64_t oldSCR
Definition: multiplex.h:73
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:442
MAX_TYPES
@ MAX_TYPES
Definition: element.h:84
ringbuffer
Definition: ringbuffer.h:39
multiplex_t::audio_buffer_size
uint32_t audio_buffer_size
Definition: multiplex.h:57
multiplex_t::vrbuffer
ringbuffer * vrbuffer
Definition: multiplex.h:84
sequence_t
Definition: element.h:87
TS_HEADER_MIN
#define TS_HEADER_MIN
Definition: pes.h:12
audio_frame_t::language
char language[4]
Definition: element.h:120
TIME_ALWAYS
#define TIME_ALWAYS
Definition: multiplex.h:62
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:59
multiplex_t::fd_out
int fd_out
Definition: multiplex.h:42
mythburn.write
def write(text, progress=True)
Definition: mythburn.py:308
audio_frame_t::bit_rate
uint32_t bit_rate
Definition: element.h:112
arg
arg(title).arg(filename).arg(doDelete))
multiplex_t::viu
index_unit viu
Definition: multiplex.h:75
write_ts_patpmt
void write_ts_patpmt(extdata_t *ext, int extcnt, uint8_t prog_num, uint8_t *buf)
Definition: ts.cpp:381
REPLEX_TS_HD
#define REPLEX_TS_HD
Definition: multiplex.h:47
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:689
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:43
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:678
sequence_t::bit_rate
uint32_t bit_rate
Definition: element.h:94
REPLEX_HDTV
#define REPLEX_HDTV
Definition: multiplex.h:45
ext_arr
std::array< extdata_t, N_AUDIO > ext_arr
Definition: multiplex.h:38
I_FRAME
#define I_FRAME
Definition: element.h:54
multiplex_t::navpack
uint8_t navpack
Definition: multiplex.h:61
multiplex_t::otype
int otype
Definition: multiplex.h:48
index_unit::seq_header
uint8_t seq_header
Definition: mpg_common.h:41
MPEG_AUDIO
@ MPEG_AUDIO
Definition: element.h:84
P_FRAME
#define P_FRAME
Definition: element.h:55
ptsdiff
int64_t ptsdiff(uint64_t pts1, uint64_t pts2)
Definition: pes.cpp:78
writeout_ext
static void writeout_ext(multiplex_t *mx, int n)
Definition: multiplex.cpp:317
PES_H_MIN
#define PES_H_MIN
Definition: pes.h:16
multiplex_t::ext
ext_arr ext
Definition: multiplex.h:79
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:784
multiplex_t::index_extrbuffer
ringbuffer * index_extrbuffer
Definition: multiplex.h:83
mythlogging.h
multiplex_t::write_end_codes
int write_end_codes
Definition: multiplex.h:68
multiplex_t::vsize
unsigned int vsize
Definition: multiplex.h:70
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:411
N_AUDIO
#define N_AUDIO
Definition: multiplex.h:35
multiplex_t::index_vrbuffer
ringbuffer * index_vrbuffer
Definition: multiplex.h:85
REPLEX_DVD
#define REPLEX_DVD
Definition: multiplex.h:44
dummy_add
int dummy_add(dummy_buffer *dbuf, uint64_t time, uint32_t size)
Definition: ringbuffer.cpp:424
multiplex_t::audio_delay
uint64_t audio_delay
Definition: multiplex.h:54
extdata_t
Definition: mpg_common.h:54
multiplex_t::VBR
int VBR
Definition: multiplex.h:65
check_times
void check_times(multiplex_t *mx, int *video_ok, aok_arr &ext_ok, int *start)
Definition: multiplex.cpp:485
f
QTextStream t & f
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:603
multiplex_t::set_broken_link
int set_broken_link
Definition: multiplex.h:69
index_unit::frame
uint8_t frame
Definition: mpg_common.h:45
multiplex_t::SCR
uint64_t SCR
Definition: multiplex.h:72
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
multiplex_t::vdbuf
dummy_buffer vdbuf
Definition: multiplex.h:77
multiplex_t::reset_clocks
int reset_clocks
Definition: multiplex.h:67
which_ext
static int which_ext(ext_arr &ext, const aok_arr &aok, int n)
Definition: multiplex.cpp:30
ring_rpos
static int ring_rpos(ringbuffer *rbuf)
Definition: ringbuffer.h:82
multiplex_t::pack_size
int pack_size
Definition: multiplex.h:55
writeout_padding
static void writeout_padding(multiplex_t *mx)
Definition: multiplex.cpp:473
index_unit::framesize
uint32_t framesize
Definition: mpg_common.h:50
multiplex_t::extra_clock
int64_t extra_clock
Definition: multiplex.h:71
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:104
ring_read
int ring_read(ringbuffer *rbuf, uint8_t *data, int count)
Definition: ringbuffer.cpp:200
dummy_buffer
Definition: ringbuffer.h:49
REPLEX_MPEG2
#define REPLEX_MPEG2
Definition: multiplex.h:43
write_out_packs
void write_out_packs(multiplex_t *mx, int video_ok, aok_arr &ext_ok)
Definition: multiplex.cpp:581
write_padding_pes
void write_padding_pes(int pack_size, int extcnt, uint64_t SCR, uint64_t muxr, uint8_t *buf)
Definition: pes.cpp:677
REPLEX_TS_SD
#define REPLEX_TS_SD
Definition: multiplex.h:46
write_nav_pack
int write_nav_pack(int pack_size, int extcnt, uint64_t SCR, uint32_t muxr, uint8_t *buf)
Definition: pes.cpp:837
use_video
static int use_video(uint64_t vpts, ext_arr &ext, const aok_arr &aok, int n)
Definition: multiplex.cpp:23
multiplex_t::extcnt
int extcnt
Definition: multiplex.h:80
ring_avail
static unsigned int ring_avail(ringbuffer *rbuf)
Definition: ringbuffer.h:108
multiplex_t::fill_buffers
int(* fill_buffers)(void *p, int f)
Definition: multiplex.h:87
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:288
multiplex_t::startup
int startup
Definition: multiplex.h:49
multiplex_t::muxr
uint32_t muxr
Definition: multiplex.h:60
multiplex_t::data_size
unsigned int data_size
Definition: multiplex.h:56
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:283
dummy_space
static uint32_t dummy_space(dummy_buffer *dbuf)
Definition: ringbuffer.h:117
multiplex_t::extsize
unsigned int extsize
Definition: multiplex.h:70
AC3
@ AC3
Definition: element.h:84
get_ts_ext_overhead
static int get_ts_ext_overhead(int pktsize, audio_frame_t *extframe, extdata_t *ext, int cnt)
Definition: multiplex.cpp:658
setup_multiplex
void setup_multiplex(multiplex_t *mx)
Definition: multiplex.cpp:837
multiplex_t::video_buffer_size
uint32_t video_buffer_size
Definition: multiplex.h:58
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:739
audio_frame_t::framesize
uint32_t framesize
Definition: element.h:117
ptscmp
int ptscmp(uint64_t pts1, uint64_t pts2)
Definition: pes.cpp:114
audio_frame_t
Definition: element.h:109
ring_poke
int ring_poke(ringbuffer *rbuf, uint8_t *data, unsigned int count, uint32_t off)
Definition: ringbuffer.cpp:161
multiplex_t
Definition: multiplex.h:41
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
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:233
PTS_DTS
#define PTS_DTS
Definition: pes.h:47
multiplex_t::priv
void * priv
Definition: multiplex.h:88
index_unit
Definition: mpg_common.h:35
multiplex_t::is_ts
int is_ts
Definition: multiplex.h:66
multiplex_t::video_delay
uint64_t video_delay
Definition: multiplex.h:53
multiplex_t::frame_timestamps
int frame_timestamps
Definition: multiplex.h:64
multiplex_t::extrbuffer
ringbuffer * extrbuffer
Definition: multiplex.h:82
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:331
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:74
aok_arr
std::array< bool, N_AUDIO > aok_arr
Definition: multiplex.h:39
index_unit::length
uint32_t length
Definition: mpg_common.h:37
TIME_IFRAME
#define TIME_IFRAME
Definition: multiplex.h:63