1 /**
2  *  Transcoding
3  *  Copyright (C) 2013 John Törnblom
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program 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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <unistd.h>
20 #include <libavformat/avformat.h>
21 #include <libavcodec/avcodec.h>
22 #include <libavfilter/avfilter.h>
23 #include <libavfilter/buffersink.h>
24 #include <libavfilter/buffersrc.h>
25 #include <libavutil/opt.h>
26 #include <libavresample/avresample.h>
27 #include <libavutil/opt.h>
28 #include <libavutil/audio_fifo.h>
29 #include <libavutil/dict.h>
30 
31 #if LIBAVUTIL_VERSION_MICRO >= 100 /* FFMPEG */
32 #define USING_FFMPEG 1
33 #endif
34 
35 #include "tvheadend.h"
36 #include "settings.h"
37 #include "streaming.h"
38 #include "service.h"
39 #include "packet.h"
40 #include "transcoding.h"
41 #include "libav.h"
42 #include "parsers/bitstream.h"
43 #include "parsers/parser_avc.h"
44 
45 LIST_HEAD(transcoder_stream_list, transcoder_stream);
46 
47 struct transcoder;
48 
49 typedef struct transcoder_stream {
50   int                           ts_index;
51   streaming_component_type_t    ts_type;
52   streaming_target_t           *ts_target;
53   LIST_ENTRY(transcoder_stream) ts_link;
54   int                           ts_first;
55 
56   pktbuf_t                     *ts_input_gh;
57 
58   void (*ts_handle_pkt) (struct transcoder *, struct transcoder_stream *, th_pkt_t *);
59   void (*ts_destroy)    (struct transcoder *, struct transcoder_stream *);
60 } transcoder_stream_t;
61 
62 
63 typedef struct audio_stream {
64   transcoder_stream_t;
65 
66   AVCodecContext *aud_ictx;
67   AVCodec        *aud_icodec;
68 
69   AVCodecContext *aud_octx;
70   AVCodec        *aud_ocodec;
71 
72   uint64_t        aud_dec_pts;
73   uint64_t        aud_enc_pts;
74 
75   int8_t          aud_channels;
76   int32_t         aud_bitrate;
77 
78   AVAudioResampleContext *resample_context;
79   AVAudioFifo     *fifo;
80   int             resample;
81   int             resample_is_open;
82 
83   enum AVSampleFormat last_sample_fmt;
84   int             last_sample_rate;
85   uint64_t        last_channel_layout;
86 
87 } audio_stream_t;
88 
89 
90 typedef struct video_stream {
91   transcoder_stream_t;
92 
93   AVCodecContext            *vid_ictx;
94   AVCodec                   *vid_icodec;
95 
96   AVCodecContext            *vid_octx;
97   AVCodec                   *vid_ocodec;
98 
99   AVFrame                   *vid_dec_frame;
100   AVFrame                   *vid_enc_frame;
101 
102   AVFilterGraph             *flt_graph;
103   AVFilterContext           *flt_bufsrcctx;
104   AVFilterContext           *flt_bufsinkctx;
105 
106   int16_t                    vid_width;
107   int16_t                    vid_height;
108 
109   int                        vid_first_sent;
110   int                        vid_first_encoded;
111   th_pkt_t                  *vid_first_pkt;
112 } video_stream_t;
113 
114 
115 typedef struct subtitle_stream {
116   transcoder_stream_t;
117 
118   AVCodecContext            *sub_ictx;
119   AVCodec                   *sub_icodec;
120 
121   AVCodecContext            *sub_octx;
122   AVCodec                   *sub_ocodec;
123 } subtitle_stream_t;
124 
125 
126 
127 typedef struct transcoder {
128   streaming_target_t  t_input;  // must be first
129   streaming_target_t *t_output;
130 
131   uint32_t            t_id;
132 
133   transcoder_props_t            t_props;
134   struct transcoder_stream_list t_stream_list;
135 } transcoder_t;
136 
137 
138 
139 #define WORKING_ENCODER(x) \
140   ((x) == AV_CODEC_ID_H264 || (x) == AV_CODEC_ID_MPEG2VIDEO || \
141    (x) == AV_CODEC_ID_VP8  || /* (x) == AV_CODEC_ID_VP9 || */ \
142    (x) == AV_CODEC_ID_HEVC || (x) == AV_CODEC_ID_AAC || \
143    (x) == AV_CODEC_ID_MP2  || (x) == AV_CODEC_ID_VORBIS)
144 
145 /**
146  *
147  */
148 static inline int
shortid(transcoder_t * t)149 shortid(transcoder_t *t)
150 {
151   return t->t_id & 0xffff;
152 }
153 
154 static inline void
transcoder_stream_invalidate(transcoder_stream_t * ts)155 transcoder_stream_invalidate(transcoder_stream_t *ts)
156 {
157   ts->ts_index = 0;
158 }
159 
160 static AVCodecContext *
avcodec_alloc_context3_tvh(const AVCodec * codec)161 avcodec_alloc_context3_tvh(const AVCodec *codec)
162 {
163   AVCodecContext *ctx = avcodec_alloc_context3(codec);
164   if (ctx) {
165     ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
166   }
167   return ctx;
168 }
169 
170 static char *const
get_error_text(const int error)171 get_error_text(const int error)
172 {
173   static char error_buffer[255];
174   av_strerror(error, error_buffer, sizeof(error_buffer));
175   return error_buffer;
176 }
177 
178 static int
transcode_opt_set_int(transcoder_t * t,transcoder_stream_t * ts,void * ctx,const char * opt,int64_t val,int abort)179 transcode_opt_set_int(transcoder_t *t, transcoder_stream_t *ts,
180                       void *ctx, const char *opt,
181                       int64_t val, int abort)
182 {
183   int opt_error;
184   if ((opt_error = av_opt_set_int(ctx, opt, val, 0)) != 0) {
185     tvherror(LS_TRANSCODE, "%04X: Could not set option %s (error '%s')",
186              shortid(t), opt, get_error_text(opt_error));
187     if (abort)
188       transcoder_stream_invalidate(ts);
189     return -1;
190   }
191   return 0;
192 }
193 
194 static void
av_dict_set_int__(AVDictionary ** opts,const char * key,int64_t val,int flags)195 av_dict_set_int__(AVDictionary **opts, const char *key, int64_t val, int flags)
196 {
197   char buf[32];
198   snprintf(buf, sizeof(buf), "%"PRId64, val);
199   av_dict_set(opts, key, buf, flags);
200 }
201 
202 /**
203  * get best effort sample rate
204  */
205 static int
transcode_get_sample_rate(int rate,AVCodec * codec)206 transcode_get_sample_rate(int rate, AVCodec *codec)
207 {
208   /* if codec only supports certain rates, check if rate is available */
209   if (codec->supported_samplerates) {
210     /* Find if we have a matching sample_rate */
211     int acount = 0;
212     int rate_alt = 0;
213     while (codec->supported_samplerates[acount] > 0) {
214       if (codec->supported_samplerates[acount] == rate) {
215         /* original rate supported by codec */
216         return rate;
217       }
218 
219       /* check for highest available rate smaller that the original rate */
220       if (codec->supported_samplerates[acount] > rate_alt &&
221           codec->supported_samplerates[acount] < rate) {
222         rate_alt = codec->supported_samplerates[acount];
223       }
224       acount++;
225     }
226 
227     return rate_alt;
228   }
229 
230 
231   return rate;
232 }
233 
234 /**
235  * get best effort sample format
236  */
237 static enum AVSampleFormat
transcode_get_sample_fmt(enum AVSampleFormat fmt,AVCodec * codec)238 transcode_get_sample_fmt(enum AVSampleFormat fmt, AVCodec *codec)
239 {
240   /* if codec only supports certain formats, check if selected format is available */
241   if (codec->sample_fmts) {
242     /* Find if we have a matching sample_fmt */
243     int acount = 0;
244     while (codec->sample_fmts[acount] > AV_SAMPLE_FMT_NONE) {
245       if (codec->sample_fmts[acount] == fmt) {
246         /* original format supported by codec */
247         return fmt;
248       }
249       acount++;
250     }
251 
252     /* use first supported sample format */
253     if (acount > 0) {
254       return codec->sample_fmts[0];
255     } else {
256       return AV_SAMPLE_FMT_NONE;
257     }
258   }
259 
260   return fmt;
261 }
262 
263 /**
264  * get best effort channel layout
265  */
266 static uint64_t
transcode_get_channel_layout(int * channels,AVCodec * codec)267 transcode_get_channel_layout(int *channels, AVCodec *codec)
268 {
269   uint64_t channel_layout = AV_CH_LAYOUT_STEREO;
270 
271   /* use channel layout based on input field */
272   switch (*channels) {
273   case 1: channel_layout = AV_CH_LAYOUT_MONO;     break;
274   case 2: channel_layout = AV_CH_LAYOUT_STEREO;   break;
275   case 3: channel_layout = AV_CH_LAYOUT_SURROUND; break;
276   case 4: channel_layout = AV_CH_LAYOUT_QUAD;     break;
277   case 5: channel_layout = AV_CH_LAYOUT_5POINT0;  break;
278   case 6: channel_layout = AV_CH_LAYOUT_5POINT1;  break;
279   case 7: channel_layout = AV_CH_LAYOUT_6POINT1;  break;
280   case 8: channel_layout = AV_CH_LAYOUT_7POINT1;  break;
281   }
282 
283   /* if codec only supports certain layouts, check if selected layout is available */
284   if (codec->channel_layouts) {
285     int acount = 0;
286     uint64_t channel_layout_def = av_get_default_channel_layout(*channels);
287     uint64_t channel_layout_alt = 0;
288 
289     while (codec->channel_layouts[acount] > 0) {
290       if (codec->channel_layouts[acount] == channel_layout) {
291         /* original layout supported by codec */
292         return channel_layout;
293       }
294 
295       /* check for best matching layout with same or less number of channels */
296       if (av_get_channel_layout_nb_channels(codec->channel_layouts[acount]) <= *channels) {
297         if (av_get_channel_layout_nb_channels(codec->channel_layouts[acount]) >
298             av_get_channel_layout_nb_channels(channel_layout_alt)) {
299           /* prefer layout with more channels */
300           channel_layout_alt = codec->channel_layouts[acount];
301         } else if (av_get_channel_layout_nb_channels(codec->channel_layouts[acount]) ==
302                    av_get_channel_layout_nb_channels(channel_layout_alt) &&
303                    codec->channel_layouts[acount] == channel_layout_def) {
304           /* prefer default layout for number of channels over alternative layout */
305           channel_layout_alt = channel_layout_def;
306         }
307       }
308 
309       acount++;
310     }
311 
312     if (channel_layout_alt) {
313       channel_layout = channel_layout_alt;
314       *channels = av_get_channel_layout_nb_channels(channel_layout_alt);
315     } else {
316       channel_layout = 0;
317       *channels = 0;
318     }
319   }
320 
321   return channel_layout;
322 }
323 
324 /**
325  *
326  */
327 static AVCodec *
transcoder_get_decoder(transcoder_t * t,streaming_component_type_t ty)328 transcoder_get_decoder(transcoder_t *t, streaming_component_type_t ty)
329 {
330   enum AVCodecID codec_id;
331   AVCodec *codec;
332 
333   /* the MP4A and AAC packet format is same, reduce to one type */
334   if (ty == SCT_MP4A)
335     ty = SCT_AAC;
336 
337   codec_id = streaming_component_type2codec_id(ty);
338   if (codec_id == AV_CODEC_ID_NONE) {
339     tvherror(LS_TRANSCODE, "%04X: Unsupported input codec %s",
340 	     shortid(t), streaming_component_type2txt(ty));
341     return NULL;
342   }
343 
344   codec = avcodec_find_decoder(codec_id);
345   if (!codec) {
346     tvherror(LS_TRANSCODE, "%04X: Unable to find %s decoder",
347 	     shortid(t), streaming_component_type2txt(ty));
348     return NULL;
349   }
350 
351   tvhtrace(LS_TRANSCODE, "%04X: Using decoder %s", shortid(t), codec->name);
352 
353   return codec;
354 }
355 
356 
357 /**
358  *
359  */
360 static AVCodec *
transcoder_get_encoder(transcoder_t * t,const char * codec_name)361 transcoder_get_encoder(transcoder_t *t, const char *codec_name)
362 {
363   AVCodec *codec;
364 
365   codec = avcodec_find_encoder_by_name(codec_name);
366   if (!codec) {
367     tvherror(LS_TRANSCODE, "%04X: Unable to find %s encoder",
368              shortid(t), codec_name);
369     return NULL;
370   }
371   tvhtrace(LS_TRANSCODE, "%04X: Using encoder %s", shortid(t), codec->name);
372 
373   return codec;
374 }
375 
376 
377 /**
378  *
379  */
380 static void
transcoder_stream_packet(transcoder_t * t,transcoder_stream_t * ts,th_pkt_t * pkt)381 transcoder_stream_packet(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
382 {
383   streaming_message_t *sm;
384 
385   tvhtrace(LS_TRANSCODE, "%04X: deliver copy (pts = %" PRIu64 ")",
386            shortid(t), pkt->pkt_pts);
387   sm = streaming_msg_create_pkt(pkt);
388   streaming_target_deliver2(ts->ts_target, sm);
389   pkt_ref_dec(pkt);
390 }
391 
392 
393 /**
394  *
395  */
396 static void
transcoder_stream_subtitle(transcoder_t * t,transcoder_stream_t * ts,th_pkt_t * pkt)397 transcoder_stream_subtitle(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
398 {
399   //streaming_message_t *sm;
400   AVCodec *icodec;
401   AVCodecContext *ictx;
402   AVPacket packet;
403   AVSubtitle sub;
404   int length, got_subtitle;
405 
406   subtitle_stream_t *ss = (subtitle_stream_t*)ts;
407 
408   ictx = ss->sub_ictx;
409   //octx = ss->sub_octx;
410 
411   icodec = ss->sub_icodec;
412   //ocodec = ss->sub_ocodec;
413 
414 
415   if (!avcodec_is_open(ictx)) {
416     if (avcodec_open2(ictx, icodec, NULL) < 0) {
417       tvherror(LS_TRANSCODE, "%04X: Unable to open %s decoder",
418                shortid(t), icodec->name);
419       transcoder_stream_invalidate(ts);
420       return;
421     }
422   }
423 
424   av_init_packet(&packet);
425   packet.data     = pktbuf_ptr(pkt->pkt_payload);
426   packet.size     = pktbuf_len(pkt->pkt_payload);
427   packet.pts      = pkt->pkt_pts;
428   packet.dts      = pkt->pkt_dts;
429   packet.duration = pkt->pkt_duration;
430 
431   memset(&sub, 0, sizeof(sub));
432 
433   length = avcodec_decode_subtitle2(ictx,  &sub, &got_subtitle, &packet);
434   if (length <= 0) {
435     if (length == AVERROR_INVALIDDATA) goto cleanup;
436     tvherror(LS_TRANSCODE, "%04X: Unable to decode subtitle (%d, %s)",
437              shortid(t), length, get_error_text(length));
438     goto cleanup;
439   }
440 
441   if (!got_subtitle)
442     goto cleanup;
443 
444   //TODO: encoding
445 
446  cleanup:
447   av_free_packet(&packet);
448   avsubtitle_free(&sub);
449 }
450 
451 static void
create_adts_header(pktbuf_t * pb,int sri,int channels)452 create_adts_header(pktbuf_t *pb, int sri, int channels)
453 {
454    bitstream_t bs;
455 
456    /* 7 bytes of ADTS header */
457    init_wbits(&bs, pktbuf_ptr(pb), 56);
458 
459    put_bits(&bs, 0xfff, 12); // Sync marker
460    put_bits(&bs, 0, 1);      // ID 0 = MPEG 4, 1 = MPEG 2
461    put_bits(&bs, 0, 2);      // Layer
462    put_bits(&bs, 1, 1);      // Protection absent
463    put_bits(&bs, 1, 2);      // AOT, 1 = AAC LC
464    put_bits(&bs, sri, 4);
465    put_bits(&bs, 1, 1);      // Private bit
466    put_bits(&bs, channels, 3);
467    put_bits(&bs, 1, 1);      // Original
468    put_bits(&bs, 1, 1);      // Copy
469 
470    put_bits(&bs, 1, 1);      // Copyright identification bit
471    put_bits(&bs, 1, 1);      // Copyright identification start
472    put_bits(&bs, pktbuf_len(pb), 13);
473    put_bits(&bs, 0x7ff, 11); // Buffer fullness
474    put_bits(&bs, 0, 2);      // RDB in frame
475 }
476 
477 /**
478  *
479  */
480 static void
transcoder_stream_audio(transcoder_t * t,transcoder_stream_t * ts,th_pkt_t * pkt)481 transcoder_stream_audio(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
482 {
483   AVCodec *icodec, *ocodec;
484   AVCodecContext *ictx, *octx;
485   AVPacket packet;
486   int length;
487   streaming_message_t *sm;
488   th_pkt_t *n;
489   audio_stream_t *as = (audio_stream_t*)ts;
490   int got_frame, got_packet_ptr;
491   AVFrame *frame = av_frame_alloc();
492   char layout_buf[100];
493   uint8_t *d;
494 
495   ictx = as->aud_ictx;
496   octx = as->aud_octx;
497 
498   icodec = as->aud_icodec;
499   ocodec = as->aud_ocodec;
500 
501   av_init_packet(&packet);
502 
503   if (!avcodec_is_open(ictx)) {
504     if (icodec->id == AV_CODEC_ID_AAC || icodec->id == AV_CODEC_ID_VORBIS) {
505       d = pktbuf_ptr(pkt->pkt_payload);
506       if (icodec->id == AV_CODEC_ID_AAC && d && pktbuf_len(pkt->pkt_payload) > 2 &&
507           d[0] == 0xff && (d[1] & 0xf0) == 0xf0) {
508         /* DTS packets have all info */
509       } else if (ts->ts_input_gh) {
510         ictx->extradata_size = pktbuf_len(ts->ts_input_gh);
511         ictx->extradata = av_malloc(ictx->extradata_size);
512         memcpy(ictx->extradata,
513                pktbuf_ptr(ts->ts_input_gh), pktbuf_len(ts->ts_input_gh));
514         tvhtrace(LS_TRANSCODE, "%04X: copy meta data for %s (len %zd)",
515                  shortid(t), icodec->id == AV_CODEC_ID_AAC ? "AAC" : "VORBIS",
516                  pktbuf_len(ts->ts_input_gh));
517       } else {
518         tvherror(LS_TRANSCODE, "%04X: missing meta data for %s",
519                  shortid(t), icodec->id == AV_CODEC_ID_AAC ? "AAC" : "VORBIS");
520       }
521     }
522 
523     if (avcodec_open2(ictx, icodec, NULL) < 0) {
524       tvherror(LS_TRANSCODE, "%04X: Unable to open %s decoder",
525                shortid(t), icodec->name);
526       transcoder_stream_invalidate(ts);
527       goto cleanup;
528     }
529 
530     as->aud_dec_pts = pkt->pkt_pts;
531   }
532 
533   if (pkt->pkt_pts > as->aud_dec_pts) {
534     tvhwarn(LS_TRANSCODE, "%04X: Detected framedrop in audio", shortid(t));
535     as->aud_enc_pts += (pkt->pkt_pts - as->aud_dec_pts);
536     as->aud_dec_pts += (pkt->pkt_pts - as->aud_dec_pts);
537   }
538 
539   packet.data     = pktbuf_ptr(pkt->pkt_payload);
540   packet.size     = pktbuf_len(pkt->pkt_payload);
541   packet.pts      = pkt->pkt_pts;
542   packet.dts      = pkt->pkt_dts;
543   packet.duration = pkt->pkt_duration;
544 
545   length = avcodec_decode_audio4(ictx, frame, &got_frame, &packet);
546   av_free_packet(&packet);
547 
548   tvhtrace(LS_TRANSCODE, "%04X: audio decode: consumed=%d size=%zu, got=%d, pts=%" PRIi64,
549            shortid(t), length, pktbuf_len(pkt->pkt_payload), got_frame, pkt->pkt_pts);
550 
551   if (length < 0) {
552     if (length == AVERROR_INVALIDDATA) goto cleanup;
553     tvherror(LS_TRANSCODE, "%04X: Unable to decode audio (%d, %s)",
554              shortid(t), length, get_error_text(length));
555     transcoder_stream_invalidate(ts);
556     goto cleanup;
557   }
558 
559   if (!got_frame) {
560     tvhtrace(LS_TRANSCODE, "%04X: Did not have a full frame in the packet", shortid(t));
561     goto cleanup;
562   }
563 
564   if (length != pktbuf_len(pkt->pkt_payload))
565     tvhwarn(LS_TRANSCODE,
566             "%04X: undecoded data (in=%zu, consumed=%d)",
567             shortid(t), pktbuf_len(pkt->pkt_payload), length);
568 
569   if (!avcodec_is_open(octx)) {
570     as->aud_enc_pts       = pkt->pkt_pts;
571     octx->sample_rate     = transcode_get_sample_rate(ictx->sample_rate, ocodec);
572     octx->sample_fmt      = transcode_get_sample_fmt(ictx->sample_fmt, ocodec);
573     octx->time_base       = ictx->time_base;
574     octx->channels        = as->aud_channels ? as->aud_channels : ictx->channels;
575     octx->channel_layout  = transcode_get_channel_layout(&octx->channels, ocodec);
576     octx->bit_rate        = as->aud_bitrate  ? as->aud_bitrate  : 0;
577     octx->flags          |= AV_CODEC_FLAG_GLOBAL_HEADER;
578 
579     if (!octx->sample_rate) {
580       tvherror(LS_TRANSCODE, "%04X: audio encoder has no suitable sample rate!", shortid(t));
581       transcoder_stream_invalidate(ts);
582       goto cleanup;
583     } else {
584       tvhdebug(LS_TRANSCODE, "%04X: using audio sample rate %d",
585                           shortid(t), octx->sample_rate);
586     }
587 
588     if (octx->sample_fmt == AV_SAMPLE_FMT_NONE) {
589       tvherror(LS_TRANSCODE, "%04X: audio encoder has no suitable sample format!", shortid(t));
590       transcoder_stream_invalidate(ts);
591       goto cleanup;
592     } else {
593       tvhdebug(LS_TRANSCODE, "%04X: using audio sample format %s",
594                           shortid(t), av_get_sample_fmt_name(octx->sample_fmt));
595     }
596 
597     if (!octx->channel_layout) {
598       tvherror(LS_TRANSCODE, "%04X: audio encoder has no suitable channel layout!", shortid(t));
599       transcoder_stream_invalidate(ts);
600       goto cleanup;
601     } else {
602       av_get_channel_layout_string(layout_buf, sizeof (layout_buf), octx->channels, octx->channel_layout);
603       tvhdebug(LS_TRANSCODE, "%04X: using audio channel layout %s",
604                           shortid(t), layout_buf);
605     }
606 
607     // Set flags and quality settings, if no bitrate was specified.
608     // The MPEG2 encoder only supports encoding with fixed bitrate.
609     // All AAC encoders should support encoding with fixed bitrate,
610     // but some don't support encoding with global_quality (vbr).
611     // All vorbis encoders support encoding with global_quality (vbr),
612     // but the built in vorbis encoder doesn't support fixed bitrate.
613     switch (ts->ts_type) {
614     case SCT_MPEG2AUDIO:
615       // use 96 kbit per channel as default
616       if (octx->bit_rate == 0) {
617         octx->bit_rate = octx->channels * 96000;
618       }
619       break;
620 
621     case SCT_AAC:
622       octx->flags |= AV_CODEC_FLAG_BITEXACT;
623       // use 64 kbit per channel as default
624       if (octx->bit_rate == 0) {
625         octx->bit_rate = octx->channels * 64000;
626       }
627       break;
628 
629     case SCT_VORBIS:
630       // use vbr with quality setting as default
631       // and also use a user specified bitrate < 16 kbit as quality setting
632       if (octx->bit_rate == 0) {
633         octx->flags |= AV_CODEC_FLAG_QSCALE;
634         octx->global_quality = 4 * FF_QP2LAMBDA;
635       } else if (t->t_props.tp_abitrate < 16) {
636         octx->flags |= AV_CODEC_FLAG_QSCALE;
637         octx->global_quality = t->t_props.tp_abitrate * FF_QP2LAMBDA;
638         octx->bit_rate = 0;
639       }
640       break;
641 
642     default:
643       break;
644     }
645 
646     if (avcodec_open2(octx, ocodec, NULL) < 0) {
647       tvherror(LS_TRANSCODE, "%04X: Unable to open %s encoder",
648                shortid(t), ocodec->name);
649       transcoder_stream_invalidate(ts);
650       goto cleanup;
651     }
652 
653     as->fifo = av_audio_fifo_alloc(octx->sample_fmt, octx->channels, 1);
654     if (!as->fifo) {
655       tvherror(LS_TRANSCODE, "%04X: Could not allocate fifo", shortid(t));
656       transcoder_stream_invalidate(ts);
657       goto cleanup;
658     }
659 
660     as->resample_context    = NULL;
661 
662     as->last_sample_rate    = ictx->sample_rate;
663     as->last_sample_fmt     = ictx->sample_fmt;
664     as->last_channel_layout = ictx->channel_layout;
665   }
666 
667   /* check for changed input format and close resampler, if changed */
668   if (as->last_sample_rate    != ictx->sample_rate    ||
669       as->last_sample_fmt     != ictx->sample_fmt     ||
670       as->last_channel_layout != ictx->channel_layout) {
671     tvhdebug(LS_TRANSCODE, "%04X: audio input format changed", shortid(t));
672 
673     as->last_sample_rate    = ictx->sample_rate;
674     as->last_sample_fmt     = ictx->sample_fmt;
675     as->last_channel_layout = ictx->channel_layout;
676 
677     if (as->resample_context) {
678       tvhdebug(LS_TRANSCODE, "%04X: stopping audio resampling", shortid(t));
679       avresample_free(&as->resample_context);
680       as->resample_context = NULL;
681       as->resample_is_open = 0;
682     }
683   }
684 
685   as->resample = (ictx->channel_layout != octx->channel_layout) ||
686                  (ictx->sample_fmt     != octx->sample_fmt)     ||
687                  (ictx->sample_rate    != octx->sample_rate);
688 
689   if (as->resample) {
690     if (!as->resample_context) {
691       if (!(as->resample_context = avresample_alloc_context())) {
692         tvherror(LS_TRANSCODE, "%04X: Could not allocate resample context", shortid(t));
693         transcoder_stream_invalidate(ts);
694         goto cleanup;
695       }
696 
697       // resample audio
698       tvhdebug(LS_TRANSCODE, "%04X: starting audio resampling", shortid(t));
699 
700       av_get_channel_layout_string(layout_buf, sizeof (layout_buf), ictx->channels, ictx->channel_layout);
701       tvhdebug(LS_TRANSCODE, "%04X: IN : channel_layout=%s, rate=%d, fmt=%s, bitrate=%"PRId64,
702                shortid(t), layout_buf, ictx->sample_rate,
703                av_get_sample_fmt_name(ictx->sample_fmt), (int64_t)ictx->bit_rate);
704 
705       av_get_channel_layout_string(layout_buf, sizeof (layout_buf), octx->channels, octx->channel_layout);
706       tvhdebug(LS_TRANSCODE, "%04X: OUT: channel_layout=%s, rate=%d, fmt=%s, bitrate=%"PRId64,
707                shortid(t), layout_buf, octx->sample_rate,
708                av_get_sample_fmt_name(octx->sample_fmt), (int64_t)octx->bit_rate);
709 
710       if (transcode_opt_set_int(t, ts, as->resample_context,
711                                 "in_channel_layout", ictx->channel_layout, 1))
712         goto cleanup;
713       if (transcode_opt_set_int(t, ts, as->resample_context,
714                                 "out_channel_layout", octx->channel_layout, 1))
715         goto cleanup;
716       if (transcode_opt_set_int(t, ts, as->resample_context,
717                                 "in_sample_rate", ictx->sample_rate, 1))
718         goto cleanup;
719       if (transcode_opt_set_int(t, ts, as->resample_context,
720                                 "out_sample_rate", octx->sample_rate, 1))
721         goto cleanup;
722       if (transcode_opt_set_int(t, ts, as->resample_context,
723                                 "in_sample_fmt", ictx->sample_fmt, 1))
724         goto cleanup;
725       if (transcode_opt_set_int(t, ts, as->resample_context,
726                                 "out_sample_fmt", octx->sample_fmt, 1))
727         goto cleanup;
728       if (avresample_open(as->resample_context) < 0) {
729         tvherror(LS_TRANSCODE, "%04X: Error avresample_open", shortid(t));
730         transcoder_stream_invalidate(ts);
731         goto cleanup;
732       }
733       as->resample_is_open = 1;
734     }
735 
736     uint8_t **output = alloca(octx->channels * sizeof(uint8_t *));
737 
738     if (av_samples_alloc(output, NULL, octx->channels, frame->nb_samples, octx->sample_fmt, 1) < 0) {
739       tvherror(LS_TRANSCODE, "%04X: av_resamples_alloc failed", shortid(t));
740       transcoder_stream_invalidate(ts);
741       goto scleanup;
742     }
743 
744     length = avresample_convert(as->resample_context, NULL, 0, frame->nb_samples,
745                                 frame->extended_data, 0, frame->nb_samples);
746     tvhtrace(LS_TRANSCODE, "%04X: avresample_convert: %d", shortid(t), length);
747     while (avresample_available(as->resample_context) > 0) {
748       length = avresample_read(as->resample_context, output, frame->nb_samples);
749 
750       if (length > 0) {
751         if (av_audio_fifo_realloc(as->fifo, av_audio_fifo_size(as->fifo) + length) < 0) {
752           tvherror(LS_TRANSCODE, "%04X: Could not reallocate FIFO", shortid(t));
753           transcoder_stream_invalidate(ts);
754           goto scleanup;
755         }
756 
757         if (av_audio_fifo_write(as->fifo, (void **)output, length) < length) {
758           tvherror(LS_TRANSCODE, "%04X: Could not write to FIFO", shortid(t));
759           goto scleanup;
760         }
761       }
762       continue;
763 
764 scleanup:
765       transcoder_stream_invalidate(ts);
766       av_freep(&output[0]);
767       goto cleanup;
768     }
769 
770     av_freep(&output[0]);
771 
772 /*  Need to find out where we are going to do this. Normally at the end.
773     int delay_samples = avresample_get_delay(as->resample_context);
774     if (delay_samples) {
775       tvhdebug(LS_TRANSCODE, "%d samples in resamples delay buffer.", delay_samples);
776       goto cleanup;
777     }
778 */
779 
780   } else {
781 
782     if (av_audio_fifo_realloc(as->fifo, av_audio_fifo_size(as->fifo) + frame->nb_samples) < 0) {
783       tvherror(LS_TRANSCODE, "%04X: Could not reallocate FIFO", shortid(t));
784       transcoder_stream_invalidate(ts);
785       goto cleanup;
786     }
787 
788     if (av_audio_fifo_write(as->fifo, (void **)frame->extended_data, frame->nb_samples) < frame->nb_samples) {
789       tvherror(LS_TRANSCODE, "%04X: Could not write to FIFO", shortid(t));
790       transcoder_stream_invalidate(ts);
791       goto cleanup;
792     }
793 
794   }
795 
796   as->aud_dec_pts += pkt->pkt_duration;
797 
798   while (av_audio_fifo_size(as->fifo) >= octx->frame_size) {
799     tvhtrace(LS_TRANSCODE, "%04X: audio loop: fifo=%d, frame=%d",
800              shortid(t), av_audio_fifo_size(as->fifo), octx->frame_size);
801 
802     av_frame_free(&frame);
803     frame = av_frame_alloc();
804     frame->nb_samples = octx->frame_size;
805     frame->format = octx->sample_fmt;
806 #if USING_FFMPEG
807     frame->channels = octx->channels;
808 #endif
809     frame->channel_layout = octx->channel_layout;
810     frame->sample_rate = octx->sample_rate;
811     if (av_frame_get_buffer(frame, 0) < 0) {
812       tvherror(LS_TRANSCODE, "%04X: Could not allocate output frame samples", shortid(t));
813       transcoder_stream_invalidate(ts);
814       goto cleanup;
815     }
816 
817     if ((length = av_audio_fifo_read(as->fifo, (void **)frame->data, octx->frame_size)) != octx->frame_size) {
818       tvherror(LS_TRANSCODE, "%04X: Could not read data from FIFO", shortid(t));
819       transcoder_stream_invalidate(ts);
820       goto cleanup;
821     }
822 
823     tvhtrace(LS_TRANSCODE, "%04X: pre-encode: linesize=%d, samples=%d, pts=%" PRIi64,
824              shortid(t), frame->linesize[0], length, as->aud_enc_pts);
825 
826     frame->pts = as->aud_enc_pts;
827     as->aud_enc_pts += (octx->frame_size * 90000) / octx->sample_rate;
828 
829     av_init_packet(&packet);
830     packet.data = NULL;
831     packet.size = 0;
832     length = avcodec_encode_audio2(octx, &packet, frame, &got_packet_ptr);
833     tvhtrace(LS_TRANSCODE, "%04X: encoded: packet=%d, ret=%d, got=%d, pts=%" PRIi64,
834              shortid(t), packet.size, length, got_packet_ptr, packet.pts);
835 
836     if ((length < 0) || (got_packet_ptr < -1)) {
837 
838       tvherror(LS_TRANSCODE, "%04X: Unable to encode audio (%d:%d)",
839                shortid(t), length, got_packet_ptr);
840       transcoder_stream_invalidate(ts);
841       goto cleanup;
842 
843     } else if (got_packet_ptr && packet.pts >= 0) {
844 
845       int extra_size = 0;
846 
847       if (ts->ts_type == SCT_AAC) {
848         /* only if ADTS header is missing, create it */
849         if (packet.size < 2 || packet.data[0] != 0xff || (packet.data[1] & 0xf0) != 0xf0)
850           extra_size = 7;
851       }
852 
853       n = pkt_alloc(ts->ts_type, NULL, packet.size + extra_size, packet.pts, packet.pts);
854       memcpy(pktbuf_ptr(n->pkt_payload) + extra_size, packet.data, packet.size);
855 
856       n->pkt_componentindex = ts->ts_index;
857       n->a.pkt_channels     = octx->channels;
858       n->a.pkt_sri          = rate_to_sri(octx->sample_rate);
859       n->pkt_duration       = packet.duration;
860 
861       if (extra_size && ts->ts_type == SCT_AAC)
862         create_adts_header(n->pkt_payload, n->a.pkt_sri, octx->channels);
863 
864       if (octx->extradata_size)
865         n->pkt_meta = pktbuf_alloc(octx->extradata, octx->extradata_size);
866 
867       tvhtrace(LS_TRANSCODE, "%04X: deliver audio (pts = %" PRIi64 ", delay = %i)",
868                shortid(t), n->pkt_pts, octx->delay);
869       sm = streaming_msg_create_pkt(n);
870       streaming_target_deliver2(ts->ts_target, sm);
871       pkt_ref_dec(n);
872     }
873 
874     av_free_packet(&packet);
875   }
876 
877  cleanup:
878 
879   av_frame_free(&frame);
880   av_free_packet(&packet);
881 
882   pkt_ref_dec(pkt);
883 }
884 
885 /**
886  * Parse MPEG2 header, simplifier version (we know what ffmpeg/libav generates
887  */
888 static void
extract_mpeg2_global_data(th_pkt_t * n,uint8_t * data,int len)889 extract_mpeg2_global_data(th_pkt_t *n, uint8_t *data, int len)
890 {
891 /*
892 From: http://en.wikipedia.org/wiki/Elementary_stream
893 Field Name 	# of bits 	Description
894 start code				32 	0x000001B3
895 Horizontal Size				12
896 Vertical Size				12
897 Aspect ratio				4
898 Frame rate code				4
899 Bit rate				18 	Actual bit rate = bit rate * 400, rounded upwards. Use 0x3FFFF for variable bit rate.
900 Marker bit				1 	Always 1.
901 VBV buf size				10 	Size of video buffer verifier = 16*1024*vbv buf size
902 constrained parameters flag		1
903 load intra quantizer matrix		1 	If bit set then intra quantizer matrix follows, otherwise use default values.
904 intra quantizer matrix			0 or 64*8
905 load non intra quantizer matrix 	1 	If bit set then non intra quantizer matrix follows.
906 non intra quantizer matrix		0 or 64*8
907 
908 Minimal of 12 bytes.
909 */
910   int hs = 12;
911 
912   if (len >= hs && RB32(data) == 0x000001b3) {  // SEQ_START_CODE
913 
914     // load intra quantizer matrix
915     if (data[hs-1] & 0x02) {
916       if (hs + 64 < len) return;
917       hs += 64;
918     }
919 
920     // load non intra quantizer matrix
921     if (data[hs-1] & 0x01) {
922       if (hs + 64 < len) return;
923       hs += 64;
924     }
925 
926     // See if we have the first EXT_START_CODE. Normally 10 bytes
927     // https://git.libav.org/?p=libav.git;a=blob;f=libavcodec/mpeg12enc.c;h=3376f1075f4b7582a8e4556e98deddab3e049dab;hb=HEAD#l272
928     if (hs + 10 <= len && RB32(data + hs) == 0x000001b5) // EXT_START_CODE
929       hs += 10;
930 
931     // See if we have the second EXT_START_CODE. Normally 12 bytes
932     // https://git.libav.org/?p=libav.git;a=blob;f=libavcodec/mpeg12enc.c;h=3376f1075f4b7582a8e4556e98deddab3e049dab;hb=HEAD#l291
933     // ffmpeg libs might have this block missing
934     if (hs + 12 <= len && RB32(data + hs) == 0x000001b5) // EXT_START_CODE
935       hs += 12;
936 
937     // See if we have the second GOP_START_CODE. Normally 31 bits == 4 bytes
938     // https://git.libav.org/?p=libav.git;a=blob;f=libavcodec/mpeg12enc.c;h=3376f1075f4b7582a8e4556e98deddab3e049dab;hb=HEAD#l304
939     if (hs + 4 <= len && RB32(data + hs) == 0x000001b8) // GOP_START_CODE
940       hs += 4;
941 
942     n->pkt_meta = pktbuf_alloc(data, hs);
943   }
944 }
945 
946 /**
947  *
948  */
949 static void
send_video_packet(transcoder_t * t,transcoder_stream_t * ts,th_pkt_t * pkt,AVPacket * epkt,AVCodecContext * octx)950 send_video_packet(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt,
951                   AVPacket *epkt, AVCodecContext *octx)
952 {
953   video_stream_t *vs = (video_stream_t*)ts;
954   streaming_message_t *sm;
955   th_pkt_t *n;
956 
957   if (epkt->size <= 0) {
958     if (epkt->size) {
959       tvherror(LS_TRANSCODE, "%04X: Unable to encode video (%d)", shortid(t), epkt->size);
960       transcoder_stream_invalidate(ts);
961     }
962 
963     return;
964   }
965 
966   if (!octx->coded_frame)
967     return;
968 
969   if ((ts->ts_type == SCT_H264 || ts->ts_type == SCT_HEVC) &&
970       octx->extradata_size &&
971       (ts->ts_first || octx->coded_frame->pict_type == AV_PICTURE_TYPE_I)) {
972     n = pkt_alloc(ts->ts_type, NULL, octx->extradata_size + epkt->size, epkt->pts, epkt->dts);
973     memcpy(pktbuf_ptr(n->pkt_payload), octx->extradata, octx->extradata_size);
974     memcpy(pktbuf_ptr(n->pkt_payload) + octx->extradata_size, epkt->data, epkt->size);
975     ts->ts_first = 0;
976   } else {
977     n = pkt_alloc(ts->ts_type, epkt->data, epkt->size, epkt->pts, epkt->dts);
978   }
979 
980   switch (octx->coded_frame->pict_type) {
981   case AV_PICTURE_TYPE_I:
982     n->v.pkt_frametype = PKT_I_FRAME;
983     break;
984 
985   case AV_PICTURE_TYPE_P:
986     n->v.pkt_frametype = PKT_P_FRAME;
987     break;
988 
989   case AV_PICTURE_TYPE_B:
990     n->v.pkt_frametype = PKT_B_FRAME;
991     break;
992 
993   default:
994     break;
995   }
996 
997   n->pkt_duration       = pkt->pkt_duration;
998   n->pkt_commercial     = pkt->pkt_commercial;
999   n->pkt_componentindex = pkt->pkt_componentindex;
1000   n->v.pkt_field        = pkt->v.pkt_field;
1001   n->v.pkt_aspect_num   = pkt->v.pkt_aspect_num;
1002   n->v.pkt_aspect_den   = pkt->v.pkt_aspect_den;
1003 
1004   if(octx->coded_frame && octx->coded_frame->pts != AV_NOPTS_VALUE) {
1005     if(n->pkt_dts != PTS_UNSET)
1006       n->pkt_dts -= n->pkt_pts;
1007 
1008     n->pkt_pts = octx->coded_frame->pts;
1009 
1010     if(n->pkt_dts != PTS_UNSET)
1011       n->pkt_dts += n->pkt_pts;
1012   }
1013 
1014   if (octx->extradata_size) {
1015     n->pkt_meta = pktbuf_alloc(octx->extradata, octx->extradata_size);
1016   } else {
1017     if (octx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1018       extract_mpeg2_global_data(n, epkt->data, epkt->size);
1019   }
1020 
1021   tvhtrace(LS_TRANSCODE, "%04X: deliver video (dts = %" PRIu64 ", pts = %" PRIu64 ")", shortid(t), n->pkt_dts, n->pkt_pts);
1022 
1023   if (!vs->vid_first_encoded) {
1024     vs->vid_first_pkt = n;
1025     vs->vid_first_encoded = 1;
1026     return;
1027   }
1028   if (vs->vid_first_pkt) {
1029     if (vs->vid_first_pkt->pkt_dts < n->pkt_dts) {
1030       sm = streaming_msg_create_pkt(vs->vid_first_pkt);
1031       streaming_target_deliver2(ts->ts_target, sm);
1032     } else {
1033       tvhtrace(LS_TRANSCODE, "%04X: video skip first packet", shortid(t));
1034     }
1035     pkt_ref_dec(vs->vid_first_pkt);
1036     vs->vid_first_pkt = NULL;
1037   }
1038 
1039   sm = streaming_msg_create_pkt(n);
1040   streaming_target_deliver2(ts->ts_target, sm);
1041   pkt_ref_dec(n);
1042 
1043 }
1044 
1045 /* create a simple deinterlacer-scaler video filter chain */
1046 static int
create_video_filter(video_stream_t * vs,transcoder_t * t,AVCodecContext * ictx,AVCodecContext * octx)1047 create_video_filter(video_stream_t *vs, transcoder_t *t,
1048                     AVCodecContext *ictx, AVCodecContext *octx)
1049 {
1050   AVFilterInOut *flt_inputs, *flt_outputs;
1051   const AVFilter *flt_bufsrc, *flt_bufsink;
1052   enum AVPixelFormat pix_fmts[] = { 0, AV_PIX_FMT_NONE };
1053   char opt[128];
1054   int err;
1055 
1056   err = 1;
1057   flt_inputs = flt_outputs = NULL;
1058   flt_bufsrc = flt_bufsink = NULL;
1059 
1060   if (vs->flt_graph)
1061     avfilter_graph_free(&vs->flt_graph);
1062 
1063   vs->flt_graph = avfilter_graph_alloc();
1064   if (!vs->flt_graph)
1065     return err;
1066 
1067   flt_inputs = avfilter_inout_alloc();
1068   if (!flt_inputs)
1069     goto out_err;
1070 
1071   flt_outputs = avfilter_inout_alloc();
1072   if (!flt_outputs)
1073     goto out_err;
1074 
1075   flt_bufsrc = avfilter_get_by_name("buffer");
1076   flt_bufsink = avfilter_get_by_name("buffersink");
1077   if (!flt_bufsrc || !flt_bufsink) {
1078     tvherror(LS_TRANSCODE, "%04X: libav default buffers unknown", shortid(t));
1079     goto out_err;
1080   }
1081 
1082   memset(opt, 0, sizeof(opt));
1083   snprintf(opt, sizeof(opt), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1084            ictx->width,
1085            ictx->height,
1086            ictx->pix_fmt,
1087            ictx->time_base.num,
1088            ictx->time_base.den,
1089            ictx->sample_aspect_ratio.num,
1090            ictx->sample_aspect_ratio.den);
1091 
1092   err = avfilter_graph_create_filter(&vs->flt_bufsrcctx, flt_bufsrc, "in",
1093                                      opt, NULL, vs->flt_graph);
1094   if (err < 0) {
1095     tvherror(LS_TRANSCODE, "%04X: fltchain IN init error", shortid(t));
1096     goto out_err;
1097   }
1098 
1099   err = avfilter_graph_create_filter(&vs->flt_bufsinkctx, flt_bufsink,
1100                                      "out", NULL, NULL, vs->flt_graph);
1101   if (err < 0) {
1102     tvherror(LS_TRANSCODE, "%04X: fltchain OUT init error", shortid(t));
1103     goto out_err;
1104   }
1105 
1106   pix_fmts[0] = octx->pix_fmt;
1107   err = av_opt_set_int_list(vs->flt_bufsinkctx, "pix_fmts", pix_fmts,
1108                             AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
1109   if (err < 0) {
1110     tvherror(LS_TRANSCODE, "%08X: fltchain cannot set output pixfmt",
1111              shortid(t));
1112     goto out_err;
1113   }
1114 
1115   flt_outputs->name = av_strdup("in");
1116   flt_outputs->filter_ctx = vs->flt_bufsrcctx;
1117   flt_outputs->pad_idx = 0;
1118   flt_outputs->next = NULL;
1119   flt_inputs->name = av_strdup("out");
1120   flt_inputs->filter_ctx = vs->flt_bufsinkctx;
1121   flt_inputs->pad_idx = 0;
1122   flt_inputs->next = NULL;
1123 
1124   /* add filters: yadif to deinterlace and a scaler */
1125   memset(opt, 0, sizeof(opt));
1126   snprintf(opt, sizeof(opt), "yadif,scale=%dx%d",
1127            octx->width,
1128            octx->height);
1129   err = avfilter_graph_parse_ptr(vs->flt_graph,
1130                                  opt,
1131                                  &flt_inputs,
1132                                  &flt_outputs,
1133                                  NULL);
1134   if (err < 0) {
1135     tvherror(LS_TRANSCODE, "%04X: failed to init filter chain", shortid(t));
1136     goto out_err;
1137   }
1138 
1139   err = avfilter_graph_config(vs->flt_graph, NULL);
1140   if (err < 0) {
1141     tvherror(LS_TRANSCODE, "%04X: failed to config filter chain", shortid(t));
1142     goto out_err;
1143   }
1144 
1145   avfilter_inout_free(&flt_inputs);
1146   avfilter_inout_free(&flt_outputs);
1147 
1148   return 0;  /* all OK */
1149 
1150 out_err:
1151   if (flt_inputs)
1152     avfilter_inout_free(&flt_inputs);
1153   if (flt_outputs)
1154     avfilter_inout_free(&flt_outputs);
1155   if (vs->flt_graph) {
1156     avfilter_graph_free(&vs->flt_graph);
1157     vs->flt_graph = NULL;
1158   }
1159 
1160   return err;
1161 }
1162 
1163 /**
1164  *
1165  */
1166 static void
transcoder_stream_video(transcoder_t * t,transcoder_stream_t * ts,th_pkt_t * pkt)1167 transcoder_stream_video(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
1168 {
1169   AVCodec *icodec, *ocodec;
1170   AVCodecContext *ictx, *octx;
1171   AVDictionary *opts;
1172   AVPacket packet, packet2;
1173   int length, ret, got_picture, got_output, got_ref;
1174   video_stream_t *vs = (video_stream_t*)ts;
1175   streaming_message_t *sm;
1176   th_pkt_t *pkt2;
1177   static int max_bitrate = INT_MAX / ((3000*10)/8);
1178 
1179   av_init_packet(&packet);
1180   av_init_packet(&packet2);
1181   packet2.data = NULL;
1182   packet2.size = 0;
1183 
1184   ictx = vs->vid_ictx;
1185   octx = vs->vid_octx;
1186 
1187   icodec = vs->vid_icodec;
1188   ocodec = vs->vid_ocodec;
1189 
1190   opts = NULL;
1191 
1192   got_ref = 0;
1193 
1194   if (!avcodec_is_open(ictx)) {
1195     if (icodec->id == AV_CODEC_ID_H264) {
1196       if (ts->ts_input_gh) {
1197         ictx->extradata_size = pktbuf_len(ts->ts_input_gh);
1198         ictx->extradata = av_malloc(ictx->extradata_size);
1199         memcpy(ictx->extradata,
1200                pktbuf_ptr(ts->ts_input_gh), pktbuf_len(ts->ts_input_gh));
1201         tvhtrace(LS_TRANSCODE, "%04X: copy meta data for H264 (len %zd)",
1202                  shortid(t), pktbuf_len(ts->ts_input_gh));
1203       }
1204     }
1205 
1206     if (avcodec_open2(ictx, icodec, NULL) < 0) {
1207       tvherror(LS_TRANSCODE, "%04X: Unable to open %s decoder", shortid(t), icodec->name);
1208       transcoder_stream_invalidate(ts);
1209       goto cleanup;
1210     }
1211   }
1212 
1213   if (!vs->vid_first_sent) {
1214     /* notify global headers that we're live */
1215     /* the video packets might be delayed */
1216     pkt2 = pkt_alloc(ts->ts_type, NULL, 0, pkt->pkt_pts, pkt->pkt_dts);
1217     pkt2->pkt_componentindex = pkt->pkt_componentindex;
1218     sm = streaming_msg_create_pkt(pkt2);
1219     streaming_target_deliver2(ts->ts_target, sm);
1220     pkt_ref_dec(pkt2);
1221     vs->vid_first_sent = 1;
1222   }
1223 
1224   packet.data     = pktbuf_ptr(pkt->pkt_payload);
1225   packet.size     = pktbuf_len(pkt->pkt_payload);
1226   packet.pts      = pkt->pkt_pts;
1227   packet.dts      = pkt->pkt_dts;
1228   packet.duration = pkt->pkt_duration;
1229 
1230   vs->vid_enc_frame->pts = packet.pts;
1231   vs->vid_enc_frame->pkt_dts = packet.dts;
1232   vs->vid_enc_frame->pkt_pts = packet.pts;
1233 
1234   vs->vid_dec_frame->pts = packet.pts;
1235   vs->vid_dec_frame->pkt_dts = packet.dts;
1236   vs->vid_dec_frame->pkt_pts = packet.pts;
1237 
1238   ictx->reordered_opaque = packet.pts;
1239 
1240   length = avcodec_decode_video2(ictx, vs->vid_dec_frame, &got_picture, &packet);
1241   if (length <= 0) {
1242     if (length == AVERROR_INVALIDDATA) goto cleanup;
1243     tvherror(LS_TRANSCODE, "%04X: Unable to decode video (%d, %s)",
1244              shortid(t), length, get_error_text(length));
1245     goto cleanup;
1246   }
1247 
1248   if (!got_picture)
1249     goto cleanup;
1250 
1251   got_ref = 1;
1252 
1253   octx->sample_aspect_ratio.num = ictx->sample_aspect_ratio.num;
1254   octx->sample_aspect_ratio.den = ictx->sample_aspect_ratio.den;
1255 
1256   vs->vid_enc_frame->sample_aspect_ratio.num = vs->vid_dec_frame->sample_aspect_ratio.num;
1257   vs->vid_enc_frame->sample_aspect_ratio.den = vs->vid_dec_frame->sample_aspect_ratio.den;
1258 
1259   if(!avcodec_is_open(octx)) {
1260     // Common settings
1261     octx->width           = vs->vid_width  ? vs->vid_width  : ictx->width;
1262     octx->height          = vs->vid_height ? vs->vid_height : ictx->height;
1263 
1264     // Encoder uses "time_base" for bitrate calculation, but "time_base" from decoder
1265     // will be deprecated in the future, therefore calculate "time_base" from "framerate" if available.
1266     octx->ticks_per_frame = ictx->ticks_per_frame;
1267     if (ictx->framerate.num == 0) {
1268       ictx->framerate.num = 30;
1269       ictx->framerate.den = 1;
1270     }
1271     if (ictx->time_base.num == 0) {
1272       ictx->time_base.num = ictx->framerate.den;
1273       ictx->time_base.den = ictx->framerate.num;
1274     }
1275     octx->framerate = ictx->framerate;
1276 #if LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 13, 100) // ffmpeg 2.5
1277     octx->time_base       = av_inv_q(av_mul_q(ictx->framerate, av_make_q(ictx->ticks_per_frame, 1)));
1278 #else
1279     octx->time_base       = ictx->time_base;
1280 #endif
1281 
1282     // set default gop size to 1 second
1283     octx->gop_size        = ceil(av_q2d(av_inv_q(av_div_q(octx->time_base, (AVRational){1, octx->ticks_per_frame}))));
1284 
1285     switch (ts->ts_type) {
1286     case SCT_MPEG2VIDEO:
1287        if (!strcmp(ocodec->name, "nvenc") || !strcmp(ocodec->name, "mpeg2_qsv"))
1288           octx->pix_fmt    = AV_PIX_FMT_NV12;
1289       else
1290           octx->pix_fmt    = AV_PIX_FMT_YUV420P;
1291 
1292       octx->flags         |= AV_CODEC_FLAG_GLOBAL_HEADER;
1293 
1294       if (t->t_props.tp_vbitrate < 64) {
1295         // encode with specified quality and optimize for low latency
1296         // valid values for quality are 2-31, smaller means better quality, use 5 as default
1297         octx->flags          |= AV_CODEC_FLAG_QSCALE;
1298         octx->global_quality  = FF_QP2LAMBDA *
1299             (t->t_props.tp_vbitrate == 0 ? 5 : MINMAX(t->t_props.tp_vbitrate, 2, 31));
1300       } else {
1301         // encode with specified bitrate and optimize for high compression
1302         octx->bit_rate        = t->t_props.tp_vbitrate * 1000;
1303         octx->rc_max_rate     = ceil(octx->bit_rate * 1.25);
1304         octx->rc_buffer_size  = octx->rc_max_rate * 3;
1305         // use gop size of 5 seconds
1306         octx->gop_size       *= 5;
1307         // activate b-frames
1308         octx->max_b_frames    = 3;
1309       }
1310 
1311       break;
1312 
1313     case SCT_VP8:
1314       octx->pix_fmt        = AV_PIX_FMT_YUV420P;
1315 
1316       // setting quality to realtime will use as much CPU for transcoding as possible,
1317       // while still encoding in realtime
1318       av_dict_set(&opts, "quality", "realtime", 0);
1319 
1320       if (t->t_props.tp_vbitrate < 64) {
1321         // encode with specified quality and optimize for low latency
1322         // valid values for quality are 1-63, smaller means better quality, use 15 as default
1323         av_dict_set_int__(&opts,      "crf", t->t_props.tp_vbitrate == 0 ? 15 : t->t_props.tp_vbitrate, 0);
1324         // bitrate setting is still required, as it's used as max rate in CQ mode
1325         // and set to a very low value by default
1326         octx->bit_rate        = 25000000;
1327       } else {
1328         // encode with specified bitrate and optimize for high compression
1329         octx->bit_rate        = t->t_props.tp_vbitrate * 1000;
1330         octx->rc_buffer_size  = octx->bit_rate * 3;
1331         // use gop size of 5 seconds
1332         octx->gop_size       *= 5;
1333       }
1334 
1335       break;
1336 
1337     case SCT_H264:
1338        if (!strcmp(ocodec->name, "nvenc") || !strcmp(ocodec->name, "h264_qsv"))
1339           octx->pix_fmt    = AV_PIX_FMT_NV12;
1340       else
1341           octx->pix_fmt    = AV_PIX_FMT_YUV420P;
1342 
1343       octx->flags         |= AV_CODEC_FLAG_GLOBAL_HEADER;
1344 
1345       // Default = "medium". We gain more encoding speed compared to the loss of quality when lowering it _slightly_.
1346       // select preset according to system performance and codec type
1347       av_dict_set(&opts, "preset",  t->t_props.tp_vcodec_preset, 0);
1348       tvhinfo(LS_TRANSCODE, "%04X: Using preset %s", shortid(t), t->t_props.tp_vcodec_preset);
1349 
1350       // All modern devices should support "high" profile
1351       //av_dict_set(&opts, "profile", "high", 0);
1352 
1353       if (t->t_props.tp_vbitrate < 64) {
1354         // encode with specified quality and optimize for low latency
1355         // valid values for quality are 1-51, smaller means better quality, use 15 as default
1356         av_dict_set_int__(&opts,      "crf", t->t_props.tp_vbitrate == 0 ? 15 : MIN(51, t->t_props.tp_vbitrate), 0);
1357         // tune "zerolatency" removes as much encoder latency as possible
1358         av_dict_set(&opts,      "tune", "zerolatency", 0);
1359       } else {
1360         // encode with specified bitrate and optimize for high compression
1361         octx->bit_rate        = t->t_props.tp_vbitrate * 1000;
1362         octx->rc_max_rate     = ceil(octx->bit_rate * 1.25);
1363         octx->rc_buffer_size  = octx->rc_max_rate * 3;
1364         // force-cfr=1 is needed for correct bitrate calculation (tune "zerolatency" also sets this)
1365         av_dict_set(&opts,      "x264opts", "force-cfr=1", 0);
1366         // use gop size of 5 seconds
1367         octx->gop_size       *= 5;
1368       }
1369 
1370       break;
1371 
1372     case SCT_HEVC:
1373       octx->pix_fmt        = AV_PIX_FMT_YUV420P;
1374       octx->flags         |= AV_CODEC_FLAG_GLOBAL_HEADER;
1375 
1376       // on all hardware ultrafast (or maybe superfast) should be safe
1377       // select preset according to system performance
1378       av_dict_set(&opts, "preset",  t->t_props.tp_vcodec_preset, 0);
1379       tvhinfo(LS_TRANSCODE, "%04X: Using preset %s", shortid(t), t->t_props.tp_vcodec_preset);
1380 
1381       // disables encoder features which tend to be bottlenecks for the decoder/player
1382       av_dict_set(&opts, "tune",   "fastdecode", 0);
1383 
1384       if (t->t_props.tp_vbitrate < 64) {
1385         // encode with specified quality
1386         // valid values for crf are 1-51, smaller means better quality
1387         // use 18 as default
1388         av_dict_set_int__(&opts, "crf", t->t_props.tp_vbitrate == 0 ? 18 : MIN(51, t->t_props.tp_vbitrate), 0);
1389 
1390         // the following is equivalent to tune=zerolatency for presets: ultra/superfast
1391         av_dict_set(&opts, "x265-params", "bframes=0",        0);
1392         av_dict_set(&opts, "x265-params", ":rc-lookahead=0",  AV_DICT_APPEND);
1393         av_dict_set(&opts, "x265-params", ":scenecut=0",      AV_DICT_APPEND);
1394         av_dict_set(&opts, "x265-params", ":frame-threads=1", AV_DICT_APPEND);
1395       } else {
1396         int bitrate, maxrate, bufsize;
1397         bitrate = (t->t_props.tp_vbitrate > max_bitrate) ? max_bitrate : t->t_props.tp_vbitrate;
1398         maxrate = ceil(bitrate * 1.25);
1399         bufsize = maxrate * 3;
1400 
1401         tvhdebug(LS_TRANSCODE, "tuning HEVC encoder for ABR rate control, "
1402                  "bitrate: %dkbps, vbv-bufsize: %dkbits, vbv-maxrate: %dkbps",
1403                  bitrate, bufsize, maxrate);
1404 
1405         // this is the same as setting --bitrate=bitrate
1406         octx->bit_rate = bitrate * 1000;
1407 
1408         av_dict_set(&opts,       "x265-params", "vbv-bufsize=",  0);
1409         av_dict_set_int__(&opts, "x265-params", bufsize,         AV_DICT_APPEND);
1410         av_dict_set(&opts,       "x265-params", ":vbv-maxrate=", AV_DICT_APPEND);
1411         av_dict_set_int__(&opts, "x265-params", maxrate,         AV_DICT_APPEND);
1412         av_dict_set(&opts,       "x265-params", ":strict-cbr=1", AV_DICT_APPEND);
1413       }
1414       // reduce key frame interface for live streaming
1415       av_dict_set(&opts, "x265-params", ":keyint=49:min-keyint=15", AV_DICT_APPEND);
1416 
1417       break;
1418 
1419     default:
1420       break;
1421     }
1422 
1423     if (avcodec_open2(octx, ocodec, &opts) < 0) {
1424       tvherror(LS_TRANSCODE, "%04X: Unable to open %s encoder",
1425                shortid(t), ocodec->name);
1426       transcoder_stream_invalidate(ts);
1427       goto cleanup;
1428     }
1429 
1430     if (create_video_filter(vs, t, ictx, octx)) {
1431       tvherror(LS_TRANSCODE, "%04X: Video filter creation failed",
1432                shortid(t));
1433       transcoder_stream_invalidate(ts);
1434       goto cleanup;
1435     }
1436   }
1437 
1438   /* push decoded frame into filter chain */
1439   if (av_buffersrc_add_frame(vs->flt_bufsrcctx, vs->vid_dec_frame) < 0) {
1440     tvherror(LS_TRANSCODE, "%04X: filter input error", shortid(t));
1441     transcoder_stream_invalidate(ts);
1442     goto cleanup;
1443   }
1444 
1445   /* and pull out a filtered frame */
1446   got_output = 0;
1447   while (1) {
1448 	ret = av_buffersink_get_frame(vs->flt_bufsinkctx, vs->vid_enc_frame);
1449 	if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1450 		break;
1451 	if (ret < 0) {
1452 		tvherror(LS_TRANSCODE, "%04X: filter output error", shortid(t));
1453 		transcoder_stream_invalidate(ts);
1454 		goto cleanup;
1455 	}
1456 
1457 	vs->vid_enc_frame->format  = octx->pix_fmt;
1458 	vs->vid_enc_frame->width   = octx->width;
1459 	vs->vid_enc_frame->height  = octx->height;
1460 
1461 	vs->vid_enc_frame->pkt_pts = vs->vid_dec_frame->pkt_pts;
1462 	vs->vid_enc_frame->pkt_dts = vs->vid_dec_frame->pkt_dts;
1463 
1464 	if (vs->vid_dec_frame->reordered_opaque != AV_NOPTS_VALUE)
1465 		vs->vid_enc_frame->pts = vs->vid_dec_frame->reordered_opaque;
1466 
1467 	else if (ictx->coded_frame && ictx->coded_frame->pts != AV_NOPTS_VALUE)
1468 		vs->vid_enc_frame->pts = vs->vid_dec_frame->pts;
1469 
1470 	ret = avcodec_encode_video2(octx, &packet2, vs->vid_enc_frame, &got_output);
1471 	if (ret < 0) {
1472 		tvherror(LS_TRANSCODE, "%04X: Error encoding frame", shortid(t));
1473 		transcoder_stream_invalidate(ts);
1474 		goto cleanup;
1475 	}
1476 	av_frame_unref(vs->vid_enc_frame);
1477   }
1478 
1479   if (got_output)
1480     send_video_packet(t, ts, pkt, &packet2, octx);
1481 
1482  cleanup:
1483   if (got_ref)
1484     av_frame_unref(vs->vid_dec_frame);
1485 
1486   av_free_packet(&packet2);
1487 
1488   av_free_packet(&packet);
1489 
1490   if(opts)
1491     av_dict_free(&opts);
1492 
1493   pkt_ref_dec(pkt);
1494 }
1495 
1496 
1497 /**
1498  *
1499  */
1500 static void
transcoder_packet(transcoder_t * t,th_pkt_t * pkt)1501 transcoder_packet(transcoder_t *t, th_pkt_t *pkt)
1502 {
1503   transcoder_stream_t *ts;
1504   streaming_message_t *sm;
1505 
1506   LIST_FOREACH(ts, &t->t_stream_list, ts_link) {
1507     if (pkt->pkt_componentindex == ts->ts_index) {
1508       if (pkt->pkt_payload) {
1509         ts->ts_handle_pkt(t, ts, pkt);
1510       } else {
1511         sm = streaming_msg_create_pkt(pkt);
1512         streaming_target_deliver2(ts->ts_target, sm);
1513         pkt_ref_dec(pkt);
1514       }
1515       return;
1516     }
1517   }
1518   pkt_ref_dec(pkt);
1519 }
1520 
1521 
1522 /**
1523  *
1524  */
1525 static void
transcoder_destroy_stream(transcoder_t * t,transcoder_stream_t * ts)1526 transcoder_destroy_stream(transcoder_t *t, transcoder_stream_t *ts)
1527 {
1528   if (ts->ts_input_gh)
1529     pktbuf_ref_dec(ts->ts_input_gh);
1530   free(ts);
1531 }
1532 
1533 
1534 /**
1535  *
1536  */
1537 static int
transcoder_init_stream(transcoder_t * t,streaming_start_component_t * ssc)1538 transcoder_init_stream(transcoder_t *t, streaming_start_component_t *ssc)
1539 {
1540   transcoder_stream_t *ts = calloc(1, sizeof(transcoder_stream_t));
1541 
1542   ts->ts_index      = ssc->ssc_index;
1543   ts->ts_type       = ssc->ssc_type;
1544   ts->ts_target     = t->t_output;
1545   ts->ts_handle_pkt = transcoder_stream_packet;
1546   ts->ts_destroy    = transcoder_destroy_stream;
1547 
1548   LIST_INSERT_HEAD(&t->t_stream_list, ts, ts_link);
1549 
1550   if(ssc->ssc_gh) {
1551     pktbuf_ref_inc(ssc->ssc_gh);
1552     ts->ts_input_gh = ssc->ssc_gh;
1553     pktbuf_ref_inc(ssc->ssc_gh);
1554   }
1555 
1556   tvhinfo(LS_TRANSCODE, "%04X: %d:%s ==> Passthrough",
1557 	  shortid(t), ssc->ssc_index,
1558 	  streaming_component_type2txt(ssc->ssc_type));
1559 
1560   return 1;
1561 }
1562 
1563 
1564 /**
1565  *
1566  */
1567 static void
transcoder_destroy_subtitle(transcoder_t * t,transcoder_stream_t * ts)1568 transcoder_destroy_subtitle(transcoder_t *t, transcoder_stream_t *ts)
1569 {
1570   subtitle_stream_t *ss = (subtitle_stream_t*)ts;
1571 
1572   if(ss->sub_ictx) {
1573     av_freep(&ss->sub_ictx->extradata);
1574     ss->sub_ictx->extradata_size = 0;
1575     avcodec_close(ss->sub_ictx);
1576     av_free(ss->sub_ictx);
1577   }
1578 
1579   if(ss->sub_octx) {
1580     avcodec_close(ss->sub_octx);
1581     av_free(ss->sub_octx);
1582   }
1583 
1584   transcoder_destroy_stream(t, ts);
1585 }
1586 
1587 
1588 /**
1589  *
1590  */
1591 static int
transcoder_init_subtitle(transcoder_t * t,streaming_start_component_t * ssc)1592 transcoder_init_subtitle(transcoder_t *t, streaming_start_component_t *ssc)
1593 {
1594   subtitle_stream_t *ss;
1595   AVCodec *icodec, *ocodec;
1596   transcoder_props_t *tp = &t->t_props;
1597   int sct;
1598 
1599   if (tp->tp_scodec[0] == '\0')
1600     return 0;
1601 
1602   else if (!strcmp(tp->tp_scodec, "copy"))
1603     return transcoder_init_stream(t, ssc);
1604 
1605   else if (!(icodec = transcoder_get_decoder(t, ssc->ssc_type)))
1606     return transcoder_init_stream(t, ssc);
1607 
1608   else if (!(ocodec = transcoder_get_encoder(t, tp->tp_scodec)))
1609     return transcoder_init_stream(t, ssc);
1610 
1611   sct = codec_id2streaming_component_type(ocodec->id);
1612 
1613   if (sct == ssc->ssc_type)
1614     return transcoder_init_stream(t, ssc);
1615 
1616   ss = calloc(1, sizeof(subtitle_stream_t));
1617 
1618   ss->ts_index      = ssc->ssc_index;
1619   ss->ts_type       = sct;
1620   ss->ts_target     = t->t_output;
1621   ss->ts_handle_pkt = transcoder_stream_subtitle;
1622   ss->ts_destroy    = transcoder_destroy_subtitle;
1623   if (ssc->ssc_gh) {
1624     ss->ts_input_gh = ssc->ssc_gh;
1625     pktbuf_ref_inc(ssc->ssc_gh);
1626   }
1627 
1628   ss->sub_icodec = icodec;
1629   ss->sub_ocodec = ocodec;
1630 
1631   ss->sub_ictx = avcodec_alloc_context3_tvh(icodec);
1632   ss->sub_octx = avcodec_alloc_context3_tvh(ocodec);
1633 
1634   LIST_INSERT_HEAD(&t->t_stream_list, (transcoder_stream_t*)ss, ts_link);
1635 
1636   tvhinfo(LS_TRANSCODE, "%04X: %d:%s ==> %s (%s)",
1637 	  shortid(t), ssc->ssc_index,
1638 	  streaming_component_type2txt(ssc->ssc_type),
1639 	  streaming_component_type2txt(ss->ts_type),
1640 	  ocodec->name);
1641 
1642   ssc->ssc_type = sct;
1643   ssc->ssc_gh = NULL;
1644 
1645   return 1;
1646 }
1647 
1648 
1649 /**
1650  *
1651  */
1652 static void
transcoder_destroy_audio(transcoder_t * t,transcoder_stream_t * ts)1653 transcoder_destroy_audio(transcoder_t *t, transcoder_stream_t *ts)
1654 {
1655   audio_stream_t *as = (audio_stream_t*)ts;
1656 
1657   if(as->aud_ictx) {
1658     av_freep(&as->aud_ictx->extradata);
1659     as->aud_ictx->extradata_size = 0;
1660     avcodec_close(as->aud_ictx);
1661     av_free(as->aud_ictx);
1662   }
1663 
1664   if(as->aud_octx) {
1665     avcodec_close(as->aud_octx);
1666     av_free(as->aud_octx);
1667   }
1668 
1669   if ((as->resample_context) && as->resample_is_open )
1670       avresample_close(as->resample_context);
1671   avresample_free(&as->resample_context);
1672 
1673   av_audio_fifo_free(as->fifo);
1674 
1675   transcoder_destroy_stream(t, ts);
1676 }
1677 
1678 
1679 /**
1680  *
1681  */
1682 static int
transcoder_init_audio(transcoder_t * t,streaming_start_component_t * ssc)1683 transcoder_init_audio(transcoder_t *t, streaming_start_component_t *ssc)
1684 {
1685   audio_stream_t *as;
1686   transcoder_stream_t *ts;
1687   AVCodec *icodec, *ocodec;
1688   transcoder_props_t *tp = &t->t_props;
1689   int sct;
1690 
1691   if (tp->tp_acodec[0] == '\0')
1692     return 0;
1693 
1694   else if (!strcmp(tp->tp_acodec, "copy"))
1695     return transcoder_init_stream(t, ssc);
1696 
1697   else if (!(icodec = transcoder_get_decoder(t, ssc->ssc_type)))
1698     return transcoder_init_stream(t, ssc);
1699 
1700   else if (!(ocodec = transcoder_get_encoder(t, tp->tp_acodec)))
1701     return transcoder_init_stream(t, ssc);
1702 
1703   LIST_FOREACH(ts, &t->t_stream_list, ts_link)
1704     if (SCT_ISAUDIO(ts->ts_type))
1705        return 0;
1706 
1707   sct = codec_id2streaming_component_type(ocodec->id);
1708 
1709   // Don't transcode to identical output codec unless the streaming profile specifies a bitrate limiter.
1710   if (sct == ssc->ssc_type && t->t_props.tp_abitrate < 16) {
1711     return transcoder_init_stream(t, ssc);
1712   }
1713 
1714   as = calloc(1, sizeof(audio_stream_t));
1715 
1716   as->ts_index      = ssc->ssc_index;
1717   as->ts_type       = sct;
1718   as->ts_target     = t->t_output;
1719   as->ts_handle_pkt = transcoder_stream_audio;
1720   as->ts_destroy    = transcoder_destroy_audio;
1721   if (ssc->ssc_gh) {
1722     as->ts_input_gh = ssc->ssc_gh;
1723     pktbuf_ref_inc(ssc->ssc_gh);
1724   }
1725 
1726   as->aud_icodec = icodec;
1727   as->aud_ocodec = ocodec;
1728 
1729   as->aud_ictx = avcodec_alloc_context3_tvh(icodec);
1730   as->aud_octx = avcodec_alloc_context3_tvh(ocodec);
1731 
1732   LIST_INSERT_HEAD(&t->t_stream_list, (transcoder_stream_t*)as, ts_link);
1733 
1734   tvhinfo(LS_TRANSCODE, "%04X: %d:%s ==> %s (%s)",
1735 	  shortid(t), ssc->ssc_index,
1736 	  streaming_component_type2txt(ssc->ssc_type),
1737 	  streaming_component_type2txt(as->ts_type),
1738 	  ocodec->name);
1739 
1740   ssc->ssc_type     = sct;
1741   ssc->ssc_gh       = NULL;
1742 
1743   if(tp->tp_channels > 0)
1744     as->aud_channels = tp->tp_channels;
1745   if(tp->tp_abitrate > 0)
1746     as->aud_bitrate = tp->tp_abitrate * 1000;
1747 
1748   as->resample_context = NULL;
1749   as->fifo = NULL;
1750   as->resample = 0;
1751 
1752   return 1;
1753 }
1754 
1755 
1756 /**
1757  *
1758  */
1759 static void
transcoder_destroy_video(transcoder_t * t,transcoder_stream_t * ts)1760 transcoder_destroy_video(transcoder_t *t, transcoder_stream_t *ts)
1761 {
1762   video_stream_t *vs = (video_stream_t*)ts;
1763 
1764   if(vs->vid_ictx) {
1765     av_freep(&vs->vid_ictx->extradata);
1766     vs->vid_ictx->extradata_size = 0;
1767     avcodec_close(vs->vid_ictx);
1768     av_free(vs->vid_ictx);
1769   }
1770 
1771   if(vs->vid_octx) {
1772     avcodec_close(vs->vid_octx);
1773     av_free(vs->vid_octx);
1774   }
1775 
1776   if(vs->vid_dec_frame)
1777     av_free(vs->vid_dec_frame);
1778 
1779   if(vs->vid_enc_frame)
1780     av_free(vs->vid_enc_frame);
1781 
1782   if (vs->vid_first_pkt)
1783     pkt_ref_dec(vs->vid_first_pkt);
1784 
1785   if (vs->flt_graph) {
1786     avfilter_graph_free(&vs->flt_graph);
1787     vs->flt_graph = NULL;
1788   }
1789 
1790   transcoder_destroy_stream(t, ts);
1791 }
1792 
1793 
1794 /**
1795  *
1796  */
1797 static int
transcoder_init_video(transcoder_t * t,streaming_start_component_t * ssc)1798 transcoder_init_video(transcoder_t *t, streaming_start_component_t *ssc)
1799 {
1800   video_stream_t *vs;
1801   AVCodec *icodec, *ocodec;
1802   transcoder_props_t *tp = &t->t_props;
1803   int sct;
1804   char *str, *token, *saveptr, codec_list[sizeof(tp->tp_src_vcodec)];
1805   int codec_match=0;
1806 
1807   strncpy(codec_list, tp->tp_src_vcodec, sizeof(tp->tp_src_vcodec)-1);
1808 
1809   tvhtrace(LS_TRANSCODE, "src_vcodec=\"%s\" ssc_type=%d (%s)\n",
1810 		  tp->tp_src_vcodec,
1811 		  ssc->ssc_type,
1812 		  streaming_component_type2txt(ssc->ssc_type));
1813 
1814   if (codec_list[0] != '\0') {
1815     for (str=codec_list; ; str = NULL) {
1816       token = strtok_r(str," ,|;" , &saveptr);
1817       if (token == NULL)
1818         break; //no match found, use profile settings
1819       if(!strcasecmp(token, streaming_component_type2txt(ssc->ssc_type))) { //match found
1820 	codec_match=1;
1821 	break;
1822       }
1823     }
1824     if (!codec_match)
1825       return transcoder_init_stream(t, ssc); //copy codec
1826   }
1827 
1828 
1829   if (tp->tp_vcodec[0] == '\0')
1830     return 0;
1831 
1832   else if (!strcmp(tp->tp_vcodec, "copy"))
1833     return transcoder_init_stream(t, ssc);
1834 
1835   else if (!(icodec = transcoder_get_decoder(t, ssc->ssc_type)))
1836     return transcoder_init_stream(t, ssc);
1837 
1838   else if (!(ocodec = transcoder_get_encoder(t, tp->tp_vcodec)))
1839     return transcoder_init_stream(t, ssc);
1840 
1841   sct = codec_id2streaming_component_type(ocodec->id);
1842 
1843   vs = calloc(1, sizeof(video_stream_t));
1844 
1845   vs->ts_index      = ssc->ssc_index;
1846   vs->ts_type       = sct;
1847   vs->ts_target     = t->t_output;
1848   vs->ts_handle_pkt = transcoder_stream_video;
1849   vs->ts_destroy    = transcoder_destroy_video;
1850   if (ssc->ssc_gh) {
1851     vs->ts_input_gh = ssc->ssc_gh;
1852     pktbuf_ref_inc(ssc->ssc_gh);
1853   }
1854 
1855   vs->vid_icodec = icodec;
1856   vs->vid_ocodec = ocodec;
1857 
1858   vs->vid_ictx = avcodec_alloc_context3_tvh(icodec);
1859   vs->vid_octx = avcodec_alloc_context3_tvh(ocodec);
1860 
1861   if (t->t_props.tp_nrprocessors)
1862     vs->vid_octx->thread_count = t->t_props.tp_nrprocessors;
1863 
1864   vs->vid_dec_frame = av_frame_alloc();
1865   vs->vid_enc_frame = av_frame_alloc();
1866 
1867   av_frame_unref(vs->vid_dec_frame);
1868   av_frame_unref(vs->vid_enc_frame);
1869 
1870   vs->flt_graph = NULL;		/* allocated in packet processor */
1871 
1872   LIST_INSERT_HEAD(&t->t_stream_list, (transcoder_stream_t*)vs, ts_link);
1873 
1874 
1875   if(tp->tp_resolution > 0) {
1876     vs->vid_height = MIN(tp->tp_resolution, ssc->ssc_height);
1877     vs->vid_height += vs->vid_height & 1; /* Must be even */
1878 
1879     double aspect = (double)ssc->ssc_width / ssc->ssc_height;
1880     vs->vid_width = vs->vid_height * aspect;
1881     vs->vid_width += vs->vid_width & 1;   /* Must be even */
1882   } else {
1883     vs->vid_height = ssc->ssc_height;
1884     vs->vid_width  = ssc->ssc_width;
1885   }
1886 
1887   tvhinfo(LS_TRANSCODE, "%04X: %d:%s %dx%d ==> %s %dx%d (%s)",
1888           shortid(t),
1889           ssc->ssc_index,
1890           streaming_component_type2txt(ssc->ssc_type),
1891           ssc->ssc_width,
1892           ssc->ssc_height,
1893           streaming_component_type2txt(vs->ts_type),
1894           vs->vid_width,
1895           vs->vid_height,
1896           ocodec->name);
1897 
1898   ssc->ssc_type   = sct;
1899   ssc->ssc_width  = vs->vid_width;
1900   ssc->ssc_height = vs->vid_height;
1901   ssc->ssc_gh     = NULL;
1902 
1903   return 1;
1904 }
1905 
1906 
1907 /**
1908  * Figure out how many streams we will use.
1909  */
1910 static int
transcoder_calc_stream_count(transcoder_t * t,streaming_start_t * ss)1911 transcoder_calc_stream_count(transcoder_t *t, streaming_start_t *ss) {
1912   int i = 0;
1913   int video = 0;
1914   int audio = 0;
1915   int subtitle = 0;
1916   streaming_start_component_t *ssc = NULL;
1917 
1918   for (i = 0; i < ss->ss_num_components; i++) {
1919     ssc = &ss->ss_components[i];
1920 
1921     if (ssc->ssc_disabled)
1922       continue;
1923 
1924     if (SCT_ISVIDEO(ssc->ssc_type)) {
1925       if (t->t_props.tp_vcodec[0] == '\0')
1926 	video = 0;
1927       else if (!strcmp(t->t_props.tp_vcodec, "copy"))
1928 	video++;
1929       else
1930 	video = 1;
1931 
1932     } else if (SCT_ISAUDIO(ssc->ssc_type)) {
1933       if (t->t_props.tp_acodec[0] == '\0')
1934 	audio = 0;
1935       else if (!strcmp(t->t_props.tp_acodec, "copy"))
1936 	audio++;
1937       else
1938 	audio = 1;
1939 
1940     } else if (SCT_ISSUBTITLE(ssc->ssc_type)) {
1941       if (t->t_props.tp_scodec[0] == '\0')
1942 	subtitle = 0;
1943       else if (!strcmp(t->t_props.tp_scodec, "copy"))
1944 	subtitle++;
1945       else
1946 	subtitle = 1;
1947     }
1948   }
1949 
1950   tvhtrace(LS_TRANSCODE, "%04X: transcoder_calc_stream_count=%d (video=%d, audio=%d, subtitle=%d)",
1951            shortid(t), (video + audio + subtitle), video, audio, subtitle);
1952 
1953 
1954   return (video + audio + subtitle);
1955 }
1956 
1957 
1958 /**
1959  *
1960  */
1961 static streaming_start_t *
transcoder_start(transcoder_t * t,streaming_start_t * src)1962 transcoder_start(transcoder_t *t, streaming_start_t *src)
1963 {
1964   int i, j, n, rc;
1965   streaming_start_t *ss;
1966   transcoder_props_t *tp = &t->t_props;
1967   char* requested_lang;
1968 
1969   n = transcoder_calc_stream_count(t, src);
1970   ss = calloc(1, (sizeof(streaming_start_t) +
1971 		  sizeof(streaming_start_component_t) * n));
1972 
1973   ss->ss_refcount       = 1;
1974   ss->ss_num_components = n;
1975   ss->ss_pcr_pid        = src->ss_pcr_pid;
1976   ss->ss_pmt_pid        = src->ss_pmt_pid;
1977   service_source_info_copy(&ss->ss_si, &src->ss_si);
1978 
1979   requested_lang = tp->tp_language;
1980 
1981   if (requested_lang[0] != '\0')
1982   {
1983       for (i = 0; i < src->ss_num_components; i++) {
1984         streaming_start_component_t *ssc_src = &src->ss_components[i];
1985         if (SCT_ISAUDIO(ssc_src->ssc_type) && !strcmp(tp->tp_language, ssc_src->ssc_lang))
1986           break;
1987       }
1988 
1989       if (i == src->ss_num_components)
1990       {
1991         tvhinfo(LS_TRANSCODE, "Could not find requestd lang [%s] in stream, using first one", tp->tp_language);
1992         requested_lang[0] = '\0';
1993       }
1994   }
1995 
1996   for (i = j = 0; i < src->ss_num_components && j < n; i++) {
1997     streaming_start_component_t *ssc_src = &src->ss_components[i];
1998     streaming_start_component_t *ssc = &ss->ss_components[j];
1999 
2000     if (ssc_src->ssc_disabled)
2001       continue;
2002 
2003     *ssc = *ssc_src;
2004 
2005     if (SCT_ISVIDEO(ssc->ssc_type))
2006       rc = transcoder_init_video(t, ssc);
2007     else if (SCT_ISAUDIO(ssc->ssc_type) && (requested_lang[0] == '\0' || !strcmp(requested_lang, ssc->ssc_lang)))
2008       rc = transcoder_init_audio(t, ssc);
2009     else if (SCT_ISSUBTITLE(ssc->ssc_type))
2010       rc = transcoder_init_subtitle(t, ssc);
2011     else
2012       rc = 0;
2013 
2014     if(!rc)
2015       tvhinfo(LS_TRANSCODE, "%04X: %d:%s ==> Filtered",
2016 	      shortid(t), ssc->ssc_index,
2017 	      streaming_component_type2txt(ssc->ssc_type));
2018     else
2019       j++;
2020   }
2021 
2022   return ss;
2023 }
2024 
2025 
2026 /**
2027  *
2028  */
2029 static void
transcoder_stop(transcoder_t * t)2030 transcoder_stop(transcoder_t *t)
2031 {
2032   transcoder_stream_t *ts;
2033 
2034   while ((ts = LIST_FIRST(&t->t_stream_list))) {
2035     LIST_REMOVE(ts, ts_link);
2036 
2037     if (ts->ts_destroy)
2038       ts->ts_destroy(t, ts);
2039   }
2040 }
2041 
2042 
2043 /**
2044  *
2045  */
2046 static void
transcoder_input(void * opaque,streaming_message_t * sm)2047 transcoder_input(void *opaque, streaming_message_t *sm)
2048 {
2049   transcoder_t *t = opaque;
2050   streaming_start_t *ss;
2051 
2052   switch (sm->sm_type) {
2053   case SMT_PACKET:
2054     transcoder_packet(t, sm->sm_data);
2055     sm->sm_data = NULL;
2056     streaming_msg_free(sm);
2057     break;
2058 
2059   case SMT_START:
2060     transcoder_stop(t);
2061     ss = transcoder_start(t, sm->sm_data);
2062     streaming_start_unref(sm->sm_data);
2063     sm->sm_data = ss;
2064 
2065     streaming_target_deliver2(t->t_output, sm);
2066     break;
2067 
2068   case SMT_STOP:
2069     transcoder_stop(t);
2070     /* Fallthrough */
2071 
2072   case SMT_GRACE:
2073   case SMT_SPEED:
2074   case SMT_SKIP:
2075   case SMT_TIMESHIFT_STATUS:
2076   case SMT_EXIT:
2077   case SMT_SERVICE_STATUS:
2078   case SMT_SIGNAL_STATUS:
2079   case SMT_DESCRAMBLE_INFO:
2080   case SMT_NOSTART:
2081   case SMT_NOSTART_WARN:
2082   case SMT_MPEGTS:
2083     streaming_target_deliver2(t->t_output, sm);
2084     break;
2085   }
2086 }
2087 
2088 static htsmsg_t *
transcoder_input_info(void * opaque,htsmsg_t * list)2089 transcoder_input_info(void *opaque, htsmsg_t *list)
2090 {
2091   transcoder_t *t = opaque;
2092   streaming_target_t *st = t->t_output;
2093   htsmsg_add_str(list, NULL, "transcoder input");
2094   return st->st_ops.st_info(st->st_opaque, list);;
2095 }
2096 
2097 static streaming_ops_t transcoder_input_ops = {
2098   .st_cb   = transcoder_input,
2099   .st_info = transcoder_input_info
2100 };
2101 
2102 
2103 
2104 /**
2105  *
2106  */
2107 streaming_target_t *
transcoder_create(streaming_target_t * output)2108 transcoder_create(streaming_target_t *output)
2109 {
2110   static uint32_t transcoder_id = 0;
2111   transcoder_t *t = calloc(1, sizeof(transcoder_t));
2112 
2113   t->t_id = ++transcoder_id;
2114   if (!t->t_id) t->t_id = ++transcoder_id;
2115   t->t_output = output;
2116 
2117   streaming_target_init(&t->t_input, &transcoder_input_ops, t, 0);
2118 
2119   return &t->t_input;
2120 }
2121 
2122 
2123 /**
2124  *
2125  */
2126 void
transcoder_set_properties(streaming_target_t * st,transcoder_props_t * props)2127 transcoder_set_properties(streaming_target_t *st,
2128 			  transcoder_props_t *props)
2129 {
2130   transcoder_t *t = (transcoder_t *)st;
2131   transcoder_props_t *tp = &t->t_props;
2132 
2133   strncpy(tp->tp_vcodec, props->tp_vcodec, sizeof(tp->tp_vcodec)-1);
2134   strncpy(tp->tp_vcodec_preset, props->tp_vcodec_preset, sizeof(tp->tp_vcodec_preset)-1);
2135   strncpy(tp->tp_acodec, props->tp_acodec, sizeof(tp->tp_acodec)-1);
2136   strncpy(tp->tp_scodec, props->tp_scodec, sizeof(tp->tp_scodec)-1);
2137   tp->tp_channels   = props->tp_channels;
2138   tp->tp_vbitrate   = props->tp_vbitrate;
2139   tp->tp_abitrate   = props->tp_abitrate;
2140   tp->tp_resolution = props->tp_resolution;
2141 
2142   memcpy(tp->tp_language, props->tp_language, 4);
2143 
2144   strncpy(tp->tp_src_vcodec, props->tp_src_vcodec, sizeof(tp->tp_src_vcodec)-1);
2145 }
2146 
2147 
2148 /**
2149  *
2150  */
2151 void
transcoder_destroy(streaming_target_t * st)2152 transcoder_destroy(streaming_target_t *st)
2153 {
2154   transcoder_t *t = (transcoder_t *)st;
2155 
2156   transcoder_stop(t);
2157   free(t);
2158 }
2159 
2160 
2161 /**
2162  *
2163  */
2164 htsmsg_t *
transcoder_get_capabilities(int experimental)2165 transcoder_get_capabilities(int experimental)
2166 {
2167   AVCodec *p = NULL;
2168   streaming_component_type_t sct;
2169   htsmsg_t *array = htsmsg_create_list(), *m;
2170   char buf[128];
2171 
2172   while ((p = av_codec_next(p))) {
2173 
2174     if (!libav_is_encoder(p))
2175       continue;
2176 
2177     if (!WORKING_ENCODER(p->id))
2178       continue;
2179 
2180     if (((p->capabilities & AV_CODEC_CAP_EXPERIMENTAL) && !experimental) ||
2181         (p->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
2182       continue;
2183     }
2184 
2185     sct = codec_id2streaming_component_type(p->id);
2186     if (sct == SCT_NONE || sct == SCT_UNKNOWN)
2187       continue;
2188 
2189     m = htsmsg_create_map();
2190     htsmsg_add_s32(m, "type", sct);
2191     htsmsg_add_u32(m, "id", p->id);
2192     htsmsg_add_str(m, "name", p->name);
2193     snprintf(buf, sizeof(buf), "%s%s",
2194              p->long_name ?: "",
2195              (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ?
2196                " (Experimental)" : "");
2197     if (buf[0] != '\0')
2198       htsmsg_add_str(m, "long_name", buf);
2199     htsmsg_add_msg(array, NULL, m);
2200   }
2201   return array;
2202 }
2203 
2204 
2205 /*
2206  *
2207  */
transcoding_init(void)2208 void transcoding_init(void)
2209 {
2210 }
2211