ffmpeg_enc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <math.h>
  19. #include <stdint.h>
  20. #include "ffmpeg.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/avutil.h"
  24. #include "libavutil/dict.h"
  25. #include "libavutil/display.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/frame.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/log.h"
  30. #include "libavutil/mem.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/rational.h"
  33. #include "libavutil/time.h"
  34. #include "libavutil/timestamp.h"
  35. #include "libavcodec/avcodec.h"
  36. struct Encoder {
  37. // combined size of all the packets received from the encoder
  38. uint64_t data_size;
  39. // number of packets received from the encoder
  40. uint64_t packets_encoded;
  41. int opened;
  42. int attach_par;
  43. Scheduler *sch;
  44. unsigned sch_idx;
  45. };
  46. // data that is local to the decoder thread and not visible outside of it
  47. typedef struct EncoderThread {
  48. AVFrame *frame;
  49. AVPacket *pkt;
  50. } EncoderThread;
  51. void enc_free(Encoder **penc)
  52. {
  53. Encoder *enc = *penc;
  54. if (!enc)
  55. return;
  56. av_freep(penc);
  57. }
  58. int enc_alloc(Encoder **penc, const AVCodec *codec,
  59. Scheduler *sch, unsigned sch_idx)
  60. {
  61. Encoder *enc;
  62. *penc = NULL;
  63. enc = av_mallocz(sizeof(*enc));
  64. if (!enc)
  65. return AVERROR(ENOMEM);
  66. enc->sch = sch;
  67. enc->sch_idx = sch_idx;
  68. *penc = enc;
  69. return 0;
  70. }
  71. static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
  72. {
  73. const AVCodecHWConfig *config;
  74. HWDevice *dev = NULL;
  75. if (frames_ref &&
  76. ((AVHWFramesContext*)frames_ref->data)->format ==
  77. ost->enc_ctx->pix_fmt) {
  78. // Matching format, will try to use hw_frames_ctx.
  79. } else {
  80. frames_ref = NULL;
  81. }
  82. for (int i = 0;; i++) {
  83. config = avcodec_get_hw_config(ost->enc_ctx->codec, i);
  84. if (!config)
  85. break;
  86. if (frames_ref &&
  87. config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
  88. (config->pix_fmt == AV_PIX_FMT_NONE ||
  89. config->pix_fmt == ost->enc_ctx->pix_fmt)) {
  90. av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
  91. "frames context (format %s) with %s encoder.\n",
  92. av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
  93. ost->enc_ctx->codec->name);
  94. ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
  95. if (!ost->enc_ctx->hw_frames_ctx)
  96. return AVERROR(ENOMEM);
  97. return 0;
  98. }
  99. if (!dev &&
  100. config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
  101. dev = hw_device_get_by_type(config->device_type);
  102. }
  103. if (dev) {
  104. av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
  105. "(type %s) with %s encoder.\n", dev->name,
  106. av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name);
  107. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  108. if (!ost->enc_ctx->hw_device_ctx)
  109. return AVERROR(ENOMEM);
  110. } else {
  111. // No device required, or no device available.
  112. }
  113. return 0;
  114. }
  115. static int set_encoder_id(OutputFile *of, OutputStream *ost)
  116. {
  117. const char *cname = ost->enc_ctx->codec->name;
  118. uint8_t *encoder_string;
  119. int encoder_string_len;
  120. if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
  121. return 0;
  122. encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
  123. encoder_string = av_mallocz(encoder_string_len);
  124. if (!encoder_string)
  125. return AVERROR(ENOMEM);
  126. if (!of->bitexact && !ost->bitexact)
  127. av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
  128. else
  129. av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
  130. av_strlcat(encoder_string, cname, encoder_string_len);
  131. av_dict_set(&ost->st->metadata, "encoder", encoder_string,
  132. AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
  133. return 0;
  134. }
  135. int enc_open(void *opaque, const AVFrame *frame)
  136. {
  137. OutputStream *ost = opaque;
  138. InputStream *ist = ost->ist;
  139. Encoder *e = ost->enc;
  140. AVCodecContext *enc_ctx = ost->enc_ctx;
  141. Decoder *dec = NULL;
  142. const AVCodec *enc = enc_ctx->codec;
  143. OutputFile *of = ost->file;
  144. FrameData *fd;
  145. int frame_samples = 0;
  146. int ret;
  147. if (e->opened)
  148. return 0;
  149. // frame is always non-NULL for audio and video
  150. av_assert0(frame || (enc->type != AVMEDIA_TYPE_VIDEO && enc->type != AVMEDIA_TYPE_AUDIO));
  151. if (frame) {
  152. av_assert0(frame->opaque_ref);
  153. fd = (FrameData*)frame->opaque_ref->data;
  154. for (int i = 0; i < frame->nb_side_data; i++) {
  155. const AVSideDataDescriptor *desc = av_frame_side_data_desc(frame->side_data[i]->type);
  156. if (!(desc->props & AV_SIDE_DATA_PROP_GLOBAL))
  157. continue;
  158. ret = av_frame_side_data_clone(&enc_ctx->decoded_side_data,
  159. &enc_ctx->nb_decoded_side_data,
  160. frame->side_data[i],
  161. AV_FRAME_SIDE_DATA_FLAG_UNIQUE);
  162. if (ret < 0)
  163. return ret;
  164. }
  165. }
  166. ret = set_encoder_id(of, ost);
  167. if (ret < 0)
  168. return ret;
  169. if (ist)
  170. dec = ist->decoder;
  171. // the timebase is chosen by filtering code
  172. if (ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO) {
  173. enc_ctx->time_base = frame->time_base;
  174. enc_ctx->framerate = fd->frame_rate_filter;
  175. ost->st->avg_frame_rate = fd->frame_rate_filter;
  176. }
  177. switch (enc_ctx->codec_type) {
  178. case AVMEDIA_TYPE_AUDIO:
  179. av_assert0(frame->format != AV_SAMPLE_FMT_NONE &&
  180. frame->sample_rate > 0 &&
  181. frame->ch_layout.nb_channels > 0);
  182. enc_ctx->sample_fmt = frame->format;
  183. enc_ctx->sample_rate = frame->sample_rate;
  184. ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout);
  185. if (ret < 0)
  186. return ret;
  187. if (ost->bits_per_raw_sample)
  188. enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
  189. else
  190. enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
  191. av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
  192. break;
  193. case AVMEDIA_TYPE_VIDEO: {
  194. av_assert0(frame->format != AV_PIX_FMT_NONE &&
  195. frame->width > 0 &&
  196. frame->height > 0);
  197. enc_ctx->width = frame->width;
  198. enc_ctx->height = frame->height;
  199. enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
  200. ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
  201. av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
  202. frame->sample_aspect_ratio;
  203. enc_ctx->pix_fmt = frame->format;
  204. if (ost->bits_per_raw_sample)
  205. enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
  206. else
  207. enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
  208. av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
  209. enc_ctx->color_range = frame->color_range;
  210. enc_ctx->color_primaries = frame->color_primaries;
  211. enc_ctx->color_trc = frame->color_trc;
  212. enc_ctx->colorspace = frame->colorspace;
  213. enc_ctx->chroma_sample_location = frame->chroma_location;
  214. if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) ||
  215. (frame->flags & AV_FRAME_FLAG_INTERLACED)
  216. #if FFMPEG_OPT_TOP
  217. || ost->top_field_first >= 0
  218. #endif
  219. ) {
  220. int top_field_first =
  221. #if FFMPEG_OPT_TOP
  222. ost->top_field_first >= 0 ?
  223. ost->top_field_first :
  224. #endif
  225. !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
  226. if (enc->id == AV_CODEC_ID_MJPEG)
  227. enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
  228. else
  229. enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
  230. } else
  231. enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
  232. break;
  233. }
  234. case AVMEDIA_TYPE_SUBTITLE:
  235. enc_ctx->time_base = AV_TIME_BASE_Q;
  236. if (!enc_ctx->width) {
  237. enc_ctx->width = ost->ist->par->width;
  238. enc_ctx->height = ost->ist->par->height;
  239. }
  240. av_assert0(dec);
  241. if (dec->subtitle_header) {
  242. /* ASS code assumes this buffer is null terminated so add extra byte. */
  243. enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
  244. if (!enc_ctx->subtitle_header)
  245. return AVERROR(ENOMEM);
  246. memcpy(enc_ctx->subtitle_header, dec->subtitle_header,
  247. dec->subtitle_header_size);
  248. enc_ctx->subtitle_header_size = dec->subtitle_header_size;
  249. }
  250. break;
  251. default:
  252. av_assert0(0);
  253. break;
  254. }
  255. if (ost->bitexact)
  256. enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  257. if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
  258. av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
  259. if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
  260. ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
  261. if (ret < 0)
  262. return ret;
  263. }
  264. av_dict_set(&ost->encoder_opts, "flags", "+frame_duration", AV_DICT_MULTIKEY);
  265. ret = hw_device_setup_for_encode(ost, frame ? frame->hw_frames_ctx : NULL);
  266. if (ret < 0) {
  267. av_log(ost, AV_LOG_ERROR,
  268. "Encoding hardware device setup failed: %s\n", av_err2str(ret));
  269. return ret;
  270. }
  271. if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
  272. if (ret != AVERROR_EXPERIMENTAL)
  273. av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
  274. "incorrect parameters such as bit_rate, rate, width or height.\n");
  275. return ret;
  276. }
  277. e->opened = 1;
  278. if (ost->enc_ctx->frame_size)
  279. frame_samples = ost->enc_ctx->frame_size;
  280. ret = check_avoptions(ost->encoder_opts);
  281. if (ret < 0)
  282. return ret;
  283. if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
  284. ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
  285. av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
  286. " It takes bits/s as argument, not kbits/s\n");
  287. ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
  288. if (ret < 0) {
  289. av_log(ost, AV_LOG_FATAL,
  290. "Error initializing the output stream codec context.\n");
  291. return ret;
  292. }
  293. // copy timebase while removing common factors
  294. if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
  295. ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
  296. ret = of_stream_init(of, ost);
  297. if (ret < 0)
  298. return ret;
  299. return frame_samples;
  300. }
  301. static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
  302. {
  303. OutputFile *of = ost->file;
  304. if (of->recording_time != INT64_MAX &&
  305. av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
  306. return 0;
  307. }
  308. return 1;
  309. }
  310. static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *sub,
  311. AVPacket *pkt)
  312. {
  313. Encoder *e = ost->enc;
  314. int subtitle_out_max_size = 1024 * 1024;
  315. int subtitle_out_size, nb, i, ret;
  316. AVCodecContext *enc;
  317. int64_t pts;
  318. if (sub->pts == AV_NOPTS_VALUE) {
  319. av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
  320. return exit_on_error ? AVERROR(EINVAL) : 0;
  321. }
  322. if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
  323. return 0;
  324. enc = ost->enc_ctx;
  325. /* Note: DVB subtitle need one packet to draw them and one other
  326. packet to clear them */
  327. /* XXX: signal it in the codec context ? */
  328. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
  329. nb = 2;
  330. else if (enc->codec_id == AV_CODEC_ID_ASS)
  331. nb = FFMAX(sub->num_rects, 1);
  332. else
  333. nb = 1;
  334. /* shift timestamp to honor -ss and make check_recording_time() work with -t */
  335. pts = sub->pts;
  336. if (of->start_time != AV_NOPTS_VALUE)
  337. pts -= of->start_time;
  338. for (i = 0; i < nb; i++) {
  339. AVSubtitle local_sub = *sub;
  340. if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
  341. return AVERROR_EOF;
  342. ret = av_new_packet(pkt, subtitle_out_max_size);
  343. if (ret < 0)
  344. return AVERROR(ENOMEM);
  345. local_sub.pts = pts;
  346. // start_display_time is required to be 0
  347. local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
  348. local_sub.end_display_time -= sub->start_display_time;
  349. local_sub.start_display_time = 0;
  350. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
  351. local_sub.num_rects = 0;
  352. else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
  353. local_sub.num_rects = 1;
  354. local_sub.rects += i;
  355. }
  356. ost->frames_encoded++;
  357. subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
  358. if (subtitle_out_size < 0) {
  359. av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
  360. return subtitle_out_size;
  361. }
  362. av_shrink_packet(pkt, subtitle_out_size);
  363. pkt->time_base = AV_TIME_BASE_Q;
  364. pkt->pts = sub->pts;
  365. pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
  366. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
  367. /* XXX: the pts correction is handled here. Maybe handling
  368. it in the codec would be better */
  369. if (i == 0)
  370. pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
  371. else
  372. pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
  373. }
  374. pkt->dts = pkt->pts;
  375. ret = sch_enc_send(e->sch, e->sch_idx, pkt);
  376. if (ret < 0) {
  377. av_packet_unref(pkt);
  378. return ret;
  379. }
  380. }
  381. return 0;
  382. }
  383. void enc_stats_write(OutputStream *ost, EncStats *es,
  384. const AVFrame *frame, const AVPacket *pkt,
  385. uint64_t frame_num)
  386. {
  387. Encoder *e = ost->enc;
  388. AVIOContext *io = es->io;
  389. AVRational tb = frame ? frame->time_base : pkt->time_base;
  390. int64_t pts = frame ? frame->pts : pkt->pts;
  391. AVRational tbi = (AVRational){ 0, 1};
  392. int64_t ptsi = INT64_MAX;
  393. const FrameData *fd = NULL;
  394. if (frame ? frame->opaque_ref : pkt->opaque_ref) {
  395. fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
  396. tbi = fd->dec.tb;
  397. ptsi = fd->dec.pts;
  398. }
  399. pthread_mutex_lock(&es->lock);
  400. for (size_t i = 0; i < es->nb_components; i++) {
  401. const EncStatsComponent *c = &es->components[i];
  402. switch (c->type) {
  403. case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
  404. case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file->index); continue;
  405. case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
  406. case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
  407. case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
  408. case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
  409. case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
  410. case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
  411. case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
  412. INFINITY : ptsi * av_q2d(tbi)); continue;
  413. case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
  414. case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
  415. }
  416. if (frame) {
  417. switch (c->type) {
  418. case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, ost->samples_encoded); continue;
  419. case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
  420. default: av_assert0(0);
  421. }
  422. } else {
  423. switch (c->type) {
  424. case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
  425. case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
  426. case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
  427. case ENC_STATS_KEYFRAME: avio_write(io, (pkt->flags & AV_PKT_FLAG_KEY) ?
  428. "K" : "N", 1); continue;
  429. case ENC_STATS_BITRATE: {
  430. double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
  431. avio_printf(io, "%g", 8.0 * pkt->size / duration);
  432. continue;
  433. }
  434. case ENC_STATS_AVG_BITRATE: {
  435. double duration = pkt->dts * av_q2d(tb);
  436. avio_printf(io, "%g", duration > 0 ? 8.0 * e->data_size / duration : -1.);
  437. continue;
  438. }
  439. default: av_assert0(0);
  440. }
  441. }
  442. }
  443. avio_w8(io, '\n');
  444. avio_flush(io);
  445. pthread_mutex_unlock(&es->lock);
  446. }
  447. static inline double psnr(double d)
  448. {
  449. return -10.0 * log10(d);
  450. }
  451. static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
  452. {
  453. Encoder *e = ost->enc;
  454. const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
  455. NULL);
  456. AVCodecContext *enc = ost->enc_ctx;
  457. enum AVPictureType pict_type;
  458. int64_t frame_number;
  459. double ti1, bitrate, avg_bitrate;
  460. double psnr_val = -1;
  461. int quality;
  462. quality = sd ? AV_RL32(sd) : -1;
  463. pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
  464. atomic_store(&ost->quality, quality);
  465. if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
  466. // FIXME the scaling assumes 8bit
  467. double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
  468. if (error >= 0 && error <= 1)
  469. psnr_val = psnr(error);
  470. }
  471. if (!write_vstats)
  472. return 0;
  473. /* this is executed just the first time update_video_stats is called */
  474. if (!vstats_file) {
  475. vstats_file = fopen(vstats_filename, "w");
  476. if (!vstats_file) {
  477. perror("fopen");
  478. return AVERROR(errno);
  479. }
  480. }
  481. frame_number = e->packets_encoded;
  482. if (vstats_version <= 1) {
  483. fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
  484. quality / (float)FF_QP2LAMBDA);
  485. } else {
  486. fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ",
  487. ost->file->index, ost->index, frame_number,
  488. quality / (float)FF_QP2LAMBDA);
  489. }
  490. if (psnr_val >= 0)
  491. fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
  492. fprintf(vstats_file,"f_size= %6d ", pkt->size);
  493. /* compute pts value */
  494. ti1 = pkt->dts * av_q2d(pkt->time_base);
  495. if (ti1 < 0.01)
  496. ti1 = 0.01;
  497. bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
  498. avg_bitrate = (double)(e->data_size * 8) / ti1 / 1000.0;
  499. fprintf(vstats_file, "s_size= %8.0fKiB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  500. (double)e->data_size / 1024, ti1, bitrate, avg_bitrate);
  501. fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
  502. return 0;
  503. }
  504. static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame,
  505. AVPacket *pkt)
  506. {
  507. Encoder *e = ost->enc;
  508. AVCodecContext *enc = ost->enc_ctx;
  509. const char *type_desc = av_get_media_type_string(enc->codec_type);
  510. const char *action = frame ? "encode" : "flush";
  511. int ret;
  512. if (frame) {
  513. FrameData *fd = frame_data(frame);
  514. if (!fd)
  515. return AVERROR(ENOMEM);
  516. fd->wallclock[LATENCY_PROBE_ENC_PRE] = av_gettime_relative();
  517. if (ost->enc_stats_pre.io)
  518. enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
  519. ost->frames_encoded);
  520. ost->frames_encoded++;
  521. ost->samples_encoded += frame->nb_samples;
  522. if (debug_ts) {
  523. av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
  524. "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
  525. type_desc,
  526. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
  527. enc->time_base.num, enc->time_base.den);
  528. }
  529. if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
  530. enc->sample_aspect_ratio = frame->sample_aspect_ratio;
  531. }
  532. update_benchmark(NULL);
  533. ret = avcodec_send_frame(enc, frame);
  534. if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
  535. av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
  536. type_desc);
  537. return ret;
  538. }
  539. while (1) {
  540. FrameData *fd;
  541. av_packet_unref(pkt);
  542. ret = avcodec_receive_packet(enc, pkt);
  543. update_benchmark("%s_%s %d.%d", action, type_desc,
  544. of->index, ost->index);
  545. pkt->time_base = enc->time_base;
  546. /* if two pass, output log on success and EOF */
  547. if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
  548. fprintf(ost->logfile, "%s", enc->stats_out);
  549. if (ret == AVERROR(EAGAIN)) {
  550. av_assert0(frame); // should never happen during flushing
  551. return 0;
  552. } else if (ret < 0) {
  553. if (ret != AVERROR_EOF)
  554. av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
  555. return ret;
  556. }
  557. fd = packet_data(pkt);
  558. if (!fd)
  559. return AVERROR(ENOMEM);
  560. fd->wallclock[LATENCY_PROBE_ENC_POST] = av_gettime_relative();
  561. // attach stream parameters to first packet if requested
  562. avcodec_parameters_free(&fd->par_enc);
  563. if (e->attach_par && !e->packets_encoded) {
  564. fd->par_enc = avcodec_parameters_alloc();
  565. if (!fd->par_enc)
  566. return AVERROR(ENOMEM);
  567. ret = avcodec_parameters_from_context(fd->par_enc, enc);
  568. if (ret < 0)
  569. return ret;
  570. }
  571. pkt->flags |= AV_PKT_FLAG_TRUSTED;
  572. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  573. ret = update_video_stats(ost, pkt, !!vstats_filename);
  574. if (ret < 0)
  575. return ret;
  576. }
  577. if (ost->enc_stats_post.io)
  578. enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
  579. e->packets_encoded);
  580. if (debug_ts) {
  581. av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
  582. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
  583. "duration:%s duration_time:%s\n",
  584. type_desc,
  585. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
  586. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
  587. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
  588. }
  589. e->data_size += pkt->size;
  590. e->packets_encoded++;
  591. ret = sch_enc_send(e->sch, e->sch_idx, pkt);
  592. if (ret < 0) {
  593. av_packet_unref(pkt);
  594. return ret;
  595. }
  596. }
  597. av_assert0(0);
  598. }
  599. static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
  600. const AVFrame *frame)
  601. {
  602. double pts_time;
  603. if (kf->ref_pts == AV_NOPTS_VALUE)
  604. kf->ref_pts = frame->pts;
  605. pts_time = (frame->pts - kf->ref_pts) * av_q2d(frame->time_base);
  606. if (kf->index < kf->nb_pts &&
  607. av_compare_ts(frame->pts, frame->time_base, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
  608. kf->index++;
  609. goto force_keyframe;
  610. } else if (kf->pexpr) {
  611. double res;
  612. kf->expr_const_values[FKF_T] = pts_time;
  613. res = av_expr_eval(kf->pexpr,
  614. kf->expr_const_values, NULL);
  615. av_log(logctx, AV_LOG_TRACE,
  616. "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
  617. kf->expr_const_values[FKF_N],
  618. kf->expr_const_values[FKF_N_FORCED],
  619. kf->expr_const_values[FKF_PREV_FORCED_N],
  620. kf->expr_const_values[FKF_T],
  621. kf->expr_const_values[FKF_PREV_FORCED_T],
  622. res);
  623. kf->expr_const_values[FKF_N] += 1;
  624. if (res) {
  625. kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
  626. kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
  627. kf->expr_const_values[FKF_N_FORCED] += 1;
  628. goto force_keyframe;
  629. }
  630. } else if (kf->type == KF_FORCE_SOURCE && (frame->flags & AV_FRAME_FLAG_KEY)) {
  631. goto force_keyframe;
  632. }
  633. return AV_PICTURE_TYPE_NONE;
  634. force_keyframe:
  635. av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
  636. return AV_PICTURE_TYPE_I;
  637. }
  638. static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
  639. {
  640. OutputFile *of = ost->file;
  641. enum AVMediaType type = ost->type;
  642. if (type == AVMEDIA_TYPE_SUBTITLE) {
  643. const AVSubtitle *subtitle = frame && frame->buf[0] ?
  644. (AVSubtitle*)frame->buf[0]->data : NULL;
  645. // no flushing for subtitles
  646. return subtitle && subtitle->num_rects ?
  647. do_subtitle_out(of, ost, subtitle, pkt) : 0;
  648. }
  649. if (frame) {
  650. if (!check_recording_time(ost, frame->pts, frame->time_base))
  651. return AVERROR_EOF;
  652. if (type == AVMEDIA_TYPE_VIDEO) {
  653. frame->quality = ost->enc_ctx->global_quality;
  654. frame->pict_type = forced_kf_apply(ost, &ost->kf, frame);
  655. #if FFMPEG_OPT_TOP
  656. if (ost->top_field_first >= 0) {
  657. frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
  658. frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * (!!ost->top_field_first);
  659. }
  660. #endif
  661. } else {
  662. if (!(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
  663. ost->enc_ctx->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
  664. av_log(ost, AV_LOG_ERROR,
  665. "Audio channel count changed and encoder does not support parameter changes\n");
  666. return 0;
  667. }
  668. }
  669. }
  670. return encode_frame(of, ost, frame, pkt);
  671. }
  672. static void enc_thread_set_name(const OutputStream *ost)
  673. {
  674. char name[16];
  675. snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file->index, ost->index,
  676. ost->enc_ctx->codec->name);
  677. ff_thread_setname(name);
  678. }
  679. static void enc_thread_uninit(EncoderThread *et)
  680. {
  681. av_packet_free(&et->pkt);
  682. av_frame_free(&et->frame);
  683. memset(et, 0, sizeof(*et));
  684. }
  685. static int enc_thread_init(EncoderThread *et)
  686. {
  687. memset(et, 0, sizeof(*et));
  688. et->frame = av_frame_alloc();
  689. if (!et->frame)
  690. goto fail;
  691. et->pkt = av_packet_alloc();
  692. if (!et->pkt)
  693. goto fail;
  694. return 0;
  695. fail:
  696. enc_thread_uninit(et);
  697. return AVERROR(ENOMEM);
  698. }
  699. int encoder_thread(void *arg)
  700. {
  701. OutputStream *ost = arg;
  702. Encoder *e = ost->enc;
  703. EncoderThread et;
  704. int ret = 0, input_status = 0;
  705. int name_set = 0;
  706. ret = enc_thread_init(&et);
  707. if (ret < 0)
  708. goto finish;
  709. /* Open the subtitle encoders immediately. AVFrame-based encoders
  710. * are opened through a callback from the scheduler once they get
  711. * their first frame
  712. *
  713. * N.B.: because the callback is called from a different thread,
  714. * enc_ctx MUST NOT be accessed before sch_enc_receive() returns
  715. * for the first time for audio/video. */
  716. if (ost->type != AVMEDIA_TYPE_VIDEO && ost->type != AVMEDIA_TYPE_AUDIO) {
  717. ret = enc_open(ost, NULL);
  718. if (ret < 0)
  719. goto finish;
  720. }
  721. while (!input_status) {
  722. input_status = sch_enc_receive(e->sch, e->sch_idx, et.frame);
  723. if (input_status < 0) {
  724. if (input_status == AVERROR_EOF) {
  725. av_log(ost, AV_LOG_VERBOSE, "Encoder thread received EOF\n");
  726. if (e->opened)
  727. break;
  728. av_log(ost, AV_LOG_ERROR, "Could not open encoder before EOF\n");
  729. ret = AVERROR(EINVAL);
  730. } else {
  731. av_log(ost, AV_LOG_ERROR, "Error receiving a frame for encoding: %s\n",
  732. av_err2str(ret));
  733. ret = input_status;
  734. }
  735. goto finish;
  736. }
  737. if (!name_set) {
  738. enc_thread_set_name(ost);
  739. name_set = 1;
  740. }
  741. ret = frame_encode(ost, et.frame, et.pkt);
  742. av_packet_unref(et.pkt);
  743. av_frame_unref(et.frame);
  744. if (ret < 0) {
  745. if (ret == AVERROR_EOF)
  746. av_log(ost, AV_LOG_VERBOSE, "Encoder returned EOF, finishing\n");
  747. else
  748. av_log(ost, AV_LOG_ERROR, "Error encoding a frame: %s\n",
  749. av_err2str(ret));
  750. break;
  751. }
  752. }
  753. // flush the encoder
  754. if (ret == 0 || ret == AVERROR_EOF) {
  755. ret = frame_encode(ost, NULL, et.pkt);
  756. if (ret < 0 && ret != AVERROR_EOF)
  757. av_log(ost, AV_LOG_ERROR, "Error flushing encoder: %s\n",
  758. av_err2str(ret));
  759. }
  760. // EOF is normal thread termination
  761. if (ret == AVERROR_EOF)
  762. ret = 0;
  763. finish:
  764. enc_thread_uninit(&et);
  765. return ret;
  766. }
  767. int enc_loopback(Encoder *enc)
  768. {
  769. enc->attach_par = 1;
  770. return enc->sch_idx;
  771. }