ffmpeg_enc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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/pixdesc.h"
  31. #include "libavutil/rational.h"
  32. #include "libavutil/timestamp.h"
  33. #include "libavcodec/avcodec.h"
  34. #include "libavformat/avformat.h"
  35. struct Encoder {
  36. AVFrame *sq_frame;
  37. // packet for receiving encoded output
  38. AVPacket *pkt;
  39. // combined size of all the packets received from the encoder
  40. uint64_t data_size;
  41. // number of packets received from the encoder
  42. uint64_t packets_encoded;
  43. int opened;
  44. };
  45. void enc_free(Encoder **penc)
  46. {
  47. Encoder *enc = *penc;
  48. if (!enc)
  49. return;
  50. av_frame_free(&enc->sq_frame);
  51. av_packet_free(&enc->pkt);
  52. av_freep(penc);
  53. }
  54. int enc_alloc(Encoder **penc, const AVCodec *codec)
  55. {
  56. Encoder *enc;
  57. *penc = NULL;
  58. enc = av_mallocz(sizeof(*enc));
  59. if (!enc)
  60. return AVERROR(ENOMEM);
  61. enc->pkt = av_packet_alloc();
  62. if (!enc->pkt)
  63. goto fail;
  64. *penc = enc;
  65. return 0;
  66. fail:
  67. enc_free(&enc);
  68. return AVERROR(ENOMEM);
  69. }
  70. static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
  71. {
  72. const AVCodecHWConfig *config;
  73. HWDevice *dev = NULL;
  74. int i;
  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 (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(OutputStream *ost, const AVFrame *frame)
  136. {
  137. InputStream *ist = ost->ist;
  138. Encoder *e = ost->enc;
  139. AVCodecContext *enc_ctx = ost->enc_ctx;
  140. AVCodecContext *dec_ctx = NULL;
  141. const AVCodec *enc = enc_ctx->codec;
  142. OutputFile *of = output_files[ost->file_index];
  143. FrameData *fd;
  144. int ret;
  145. if (e->opened)
  146. return 0;
  147. // frame is always non-NULL for audio and video
  148. av_assert0(frame || (enc->type != AVMEDIA_TYPE_VIDEO && enc->type != AVMEDIA_TYPE_AUDIO));
  149. if (frame) {
  150. av_assert0(frame->opaque_ref);
  151. fd = (FrameData*)frame->opaque_ref->data;
  152. }
  153. ret = set_encoder_id(output_files[ost->file_index], ost);
  154. if (ret < 0)
  155. return ret;
  156. if (ist) {
  157. dec_ctx = ist->dec_ctx;
  158. }
  159. // the timebase is chosen by filtering code
  160. if (ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO) {
  161. enc_ctx->time_base = frame->time_base;
  162. enc_ctx->framerate = fd->frame_rate_filter;
  163. ost->st->avg_frame_rate = fd->frame_rate_filter;
  164. }
  165. switch (enc_ctx->codec_type) {
  166. case AVMEDIA_TYPE_AUDIO:
  167. enc_ctx->sample_fmt = frame->format;
  168. enc_ctx->sample_rate = frame->sample_rate;
  169. ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout);
  170. if (ret < 0)
  171. return ret;
  172. if (ost->bits_per_raw_sample)
  173. enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
  174. else
  175. enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
  176. av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
  177. break;
  178. case AVMEDIA_TYPE_VIDEO: {
  179. enc_ctx->width = frame->width;
  180. enc_ctx->height = frame->height;
  181. enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
  182. ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
  183. av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
  184. frame->sample_aspect_ratio;
  185. enc_ctx->pix_fmt = frame->format;
  186. if (ost->bits_per_raw_sample)
  187. enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
  188. else
  189. enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
  190. av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
  191. enc_ctx->color_range = frame->color_range;
  192. enc_ctx->color_primaries = frame->color_primaries;
  193. enc_ctx->color_trc = frame->color_trc;
  194. enc_ctx->colorspace = frame->colorspace;
  195. enc_ctx->chroma_sample_location = frame->chroma_location;
  196. if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) ||
  197. (frame->flags & AV_FRAME_FLAG_INTERLACED)
  198. #if FFMPEG_OPT_TOP
  199. || ost->top_field_first >= 0
  200. #endif
  201. ) {
  202. int top_field_first =
  203. #if FFMPEG_OPT_TOP
  204. ost->top_field_first >= 0 ?
  205. ost->top_field_first :
  206. #endif
  207. !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
  208. if (enc->id == AV_CODEC_ID_MJPEG)
  209. enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
  210. else
  211. enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
  212. } else
  213. enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
  214. break;
  215. }
  216. case AVMEDIA_TYPE_SUBTITLE:
  217. if (ost->enc_timebase.num)
  218. av_log(ost, AV_LOG_WARNING,
  219. "-enc_time_base not supported for subtitles, ignoring\n");
  220. enc_ctx->time_base = AV_TIME_BASE_Q;
  221. if (!enc_ctx->width) {
  222. enc_ctx->width = ost->ist->par->width;
  223. enc_ctx->height = ost->ist->par->height;
  224. }
  225. if (dec_ctx && dec_ctx->subtitle_header) {
  226. /* ASS code assumes this buffer is null terminated so add extra byte. */
  227. enc_ctx->subtitle_header = av_mallocz(dec_ctx->subtitle_header_size + 1);
  228. if (!enc_ctx->subtitle_header)
  229. return AVERROR(ENOMEM);
  230. memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
  231. dec_ctx->subtitle_header_size);
  232. enc_ctx->subtitle_header_size = dec_ctx->subtitle_header_size;
  233. }
  234. break;
  235. default:
  236. av_assert0(0);
  237. break;
  238. }
  239. if (ost->bitexact)
  240. enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  241. if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
  242. av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
  243. if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
  244. ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
  245. if (ret < 0)
  246. return ret;
  247. }
  248. av_dict_set(&ost->encoder_opts, "flags", "+frame_duration", AV_DICT_MULTIKEY);
  249. ret = hw_device_setup_for_encode(ost, frame ? frame->hw_frames_ctx : NULL);
  250. if (ret < 0) {
  251. av_log(ost, AV_LOG_ERROR,
  252. "Encoding hardware device setup failed: %s\n", av_err2str(ret));
  253. return ret;
  254. }
  255. if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
  256. if (ret != AVERROR_EXPERIMENTAL)
  257. av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
  258. "incorrect parameters such as bit_rate, rate, width or height.\n");
  259. return ret;
  260. }
  261. e->opened = 1;
  262. if (ost->sq_idx_encode >= 0) {
  263. e->sq_frame = av_frame_alloc();
  264. if (!e->sq_frame)
  265. return AVERROR(ENOMEM);
  266. }
  267. if (ost->enc_ctx->frame_size) {
  268. av_assert0(ost->sq_idx_encode >= 0);
  269. sq_frame_samples(output_files[ost->file_index]->sq_encode,
  270. ost->sq_idx_encode, ost->enc_ctx->frame_size);
  271. }
  272. ret = check_avoptions(ost->encoder_opts);
  273. if (ret < 0)
  274. return ret;
  275. if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
  276. ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
  277. av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
  278. " It takes bits/s as argument, not kbits/s\n");
  279. ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
  280. if (ret < 0) {
  281. av_log(ost, AV_LOG_FATAL,
  282. "Error initializing the output stream codec context.\n");
  283. return ret;
  284. }
  285. /*
  286. * Add global input side data. For now this is naive, and copies it
  287. * from the input stream's global side data. All side data should
  288. * really be funneled over AVFrame and libavfilter, then added back to
  289. * packet side data, and then potentially using the first packet for
  290. * global side data.
  291. */
  292. if (ist) {
  293. int i;
  294. for (i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) {
  295. AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i];
  296. if (sd_src->type != AV_PKT_DATA_CPB_PROPERTIES) {
  297. AVPacketSideData *sd_dst = av_packet_side_data_new(&ost->par_in->coded_side_data,
  298. &ost->par_in->nb_coded_side_data,
  299. sd_src->type, sd_src->size, 0);
  300. if (!sd_dst)
  301. return AVERROR(ENOMEM);
  302. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  303. if (ist->autorotate && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
  304. av_display_rotation_set((int32_t *)sd_dst->data, 0);
  305. }
  306. }
  307. }
  308. // copy timebase while removing common factors
  309. if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
  310. ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
  311. ret = of_stream_init(of, ost);
  312. if (ret < 0)
  313. return ret;
  314. return 0;
  315. }
  316. static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
  317. {
  318. OutputFile *of = output_files[ost->file_index];
  319. if (of->recording_time != INT64_MAX &&
  320. av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
  321. close_output_stream(ost);
  322. return 0;
  323. }
  324. return 1;
  325. }
  326. int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub)
  327. {
  328. Encoder *e = ost->enc;
  329. int subtitle_out_max_size = 1024 * 1024;
  330. int subtitle_out_size, nb, i, ret;
  331. AVCodecContext *enc;
  332. AVPacket *pkt = e->pkt;
  333. int64_t pts;
  334. if (sub->pts == AV_NOPTS_VALUE) {
  335. av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
  336. return exit_on_error ? AVERROR(EINVAL) : 0;
  337. }
  338. if (ost->finished ||
  339. (of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
  340. return 0;
  341. enc = ost->enc_ctx;
  342. /* Note: DVB subtitle need one packet to draw them and one other
  343. packet to clear them */
  344. /* XXX: signal it in the codec context ? */
  345. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
  346. nb = 2;
  347. else if (enc->codec_id == AV_CODEC_ID_ASS)
  348. nb = FFMAX(sub->num_rects, 1);
  349. else
  350. nb = 1;
  351. /* shift timestamp to honor -ss and make check_recording_time() work with -t */
  352. pts = sub->pts;
  353. if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
  354. pts -= output_files[ost->file_index]->start_time;
  355. for (i = 0; i < nb; i++) {
  356. AVSubtitle local_sub = *sub;
  357. if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
  358. return 0;
  359. ret = av_new_packet(pkt, subtitle_out_max_size);
  360. if (ret < 0)
  361. return AVERROR(ENOMEM);
  362. local_sub.pts = pts;
  363. // start_display_time is required to be 0
  364. local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
  365. local_sub.end_display_time -= sub->start_display_time;
  366. local_sub.start_display_time = 0;
  367. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
  368. local_sub.num_rects = 0;
  369. else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
  370. local_sub.num_rects = 1;
  371. local_sub.rects += i;
  372. }
  373. ost->frames_encoded++;
  374. subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
  375. if (subtitle_out_size < 0) {
  376. av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
  377. return subtitle_out_size;
  378. }
  379. av_shrink_packet(pkt, subtitle_out_size);
  380. pkt->time_base = AV_TIME_BASE_Q;
  381. pkt->pts = sub->pts;
  382. pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
  383. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
  384. /* XXX: the pts correction is handled here. Maybe handling
  385. it in the codec would be better */
  386. if (i == 0)
  387. pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
  388. else
  389. pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
  390. }
  391. pkt->dts = pkt->pts;
  392. ret = of_output_packet(of, ost, pkt);
  393. if (ret < 0)
  394. return ret;
  395. }
  396. return 0;
  397. }
  398. void enc_stats_write(OutputStream *ost, EncStats *es,
  399. const AVFrame *frame, const AVPacket *pkt,
  400. uint64_t frame_num)
  401. {
  402. Encoder *e = ost->enc;
  403. AVIOContext *io = es->io;
  404. AVRational tb = frame ? frame->time_base : pkt->time_base;
  405. int64_t pts = frame ? frame->pts : pkt->pts;
  406. AVRational tbi = (AVRational){ 0, 1};
  407. int64_t ptsi = INT64_MAX;
  408. const FrameData *fd;
  409. if ((frame && frame->opaque_ref) || (pkt && pkt->opaque_ref)) {
  410. fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
  411. tbi = fd->dec.tb;
  412. ptsi = fd->dec.pts;
  413. }
  414. for (size_t i = 0; i < es->nb_components; i++) {
  415. const EncStatsComponent *c = &es->components[i];
  416. switch (c->type) {
  417. case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
  418. case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file_index); continue;
  419. case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
  420. case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
  421. case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
  422. case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
  423. case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
  424. case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
  425. case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
  426. INFINITY : ptsi * av_q2d(tbi)); continue;
  427. case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
  428. case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
  429. }
  430. if (frame) {
  431. switch (c->type) {
  432. case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, ost->samples_encoded); continue;
  433. case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
  434. default: av_assert0(0);
  435. }
  436. } else {
  437. switch (c->type) {
  438. case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
  439. case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
  440. case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
  441. case ENC_STATS_BITRATE: {
  442. double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
  443. avio_printf(io, "%g", 8.0 * pkt->size / duration);
  444. continue;
  445. }
  446. case ENC_STATS_AVG_BITRATE: {
  447. double duration = pkt->dts * av_q2d(tb);
  448. avio_printf(io, "%g", duration > 0 ? 8.0 * e->data_size / duration : -1.);
  449. continue;
  450. }
  451. default: av_assert0(0);
  452. }
  453. }
  454. }
  455. avio_w8(io, '\n');
  456. avio_flush(io);
  457. }
  458. static inline double psnr(double d)
  459. {
  460. return -10.0 * log10(d);
  461. }
  462. static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
  463. {
  464. Encoder *e = ost->enc;
  465. const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
  466. NULL);
  467. AVCodecContext *enc = ost->enc_ctx;
  468. enum AVPictureType pict_type;
  469. int64_t frame_number;
  470. double ti1, bitrate, avg_bitrate;
  471. double psnr_val = -1;
  472. ost->quality = sd ? AV_RL32(sd) : -1;
  473. pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
  474. if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
  475. // FIXME the scaling assumes 8bit
  476. double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
  477. if (error >= 0 && error <= 1)
  478. psnr_val = psnr(error);
  479. }
  480. if (!write_vstats)
  481. return 0;
  482. /* this is executed just the first time update_video_stats is called */
  483. if (!vstats_file) {
  484. vstats_file = fopen(vstats_filename, "w");
  485. if (!vstats_file) {
  486. perror("fopen");
  487. return AVERROR(errno);
  488. }
  489. }
  490. frame_number = e->packets_encoded;
  491. if (vstats_version <= 1) {
  492. fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
  493. ost->quality / (float)FF_QP2LAMBDA);
  494. } else {
  495. fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
  496. ost->quality / (float)FF_QP2LAMBDA);
  497. }
  498. if (psnr_val >= 0)
  499. fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
  500. fprintf(vstats_file,"f_size= %6d ", pkt->size);
  501. /* compute pts value */
  502. ti1 = pkt->dts * av_q2d(pkt->time_base);
  503. if (ti1 < 0.01)
  504. ti1 = 0.01;
  505. bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
  506. avg_bitrate = (double)(e->data_size * 8) / ti1 / 1000.0;
  507. fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  508. (double)e->data_size / 1024, ti1, bitrate, avg_bitrate);
  509. fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
  510. return 0;
  511. }
  512. static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
  513. {
  514. Encoder *e = ost->enc;
  515. AVCodecContext *enc = ost->enc_ctx;
  516. AVPacket *pkt = e->pkt;
  517. const char *type_desc = av_get_media_type_string(enc->codec_type);
  518. const char *action = frame ? "encode" : "flush";
  519. int ret;
  520. if (frame) {
  521. if (ost->enc_stats_pre.io)
  522. enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
  523. ost->frames_encoded);
  524. ost->frames_encoded++;
  525. ost->samples_encoded += frame->nb_samples;
  526. if (debug_ts) {
  527. av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
  528. "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
  529. type_desc,
  530. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
  531. enc->time_base.num, enc->time_base.den);
  532. }
  533. if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
  534. enc->sample_aspect_ratio = frame->sample_aspect_ratio;
  535. }
  536. update_benchmark(NULL);
  537. ret = avcodec_send_frame(enc, frame);
  538. if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
  539. av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
  540. type_desc);
  541. return ret;
  542. }
  543. while (1) {
  544. av_packet_unref(pkt);
  545. ret = avcodec_receive_packet(enc, pkt);
  546. update_benchmark("%s_%s %d.%d", action, type_desc,
  547. ost->file_index, ost->index);
  548. pkt->time_base = enc->time_base;
  549. /* if two pass, output log on success and EOF */
  550. if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
  551. fprintf(ost->logfile, "%s", enc->stats_out);
  552. if (ret == AVERROR(EAGAIN)) {
  553. av_assert0(frame); // should never happen during flushing
  554. return 0;
  555. } else if (ret == AVERROR_EOF) {
  556. ret = of_output_packet(of, ost, NULL);
  557. return ret < 0 ? ret : AVERROR_EOF;
  558. } else if (ret < 0) {
  559. av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
  560. return ret;
  561. }
  562. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  563. ret = update_video_stats(ost, pkt, !!vstats_filename);
  564. if (ret < 0)
  565. return ret;
  566. }
  567. if (ost->enc_stats_post.io)
  568. enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
  569. e->packets_encoded);
  570. if (debug_ts) {
  571. av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
  572. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
  573. "duration:%s duration_time:%s\n",
  574. type_desc,
  575. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
  576. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
  577. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
  578. }
  579. if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) {
  580. av_log(NULL, AV_LOG_ERROR,
  581. "Subtitle heartbeat logic failed in %s! (%s)\n",
  582. __func__, av_err2str(ret));
  583. return ret;
  584. }
  585. e->data_size += pkt->size;
  586. e->packets_encoded++;
  587. ret = of_output_packet(of, ost, pkt);
  588. if (ret < 0)
  589. return ret;
  590. }
  591. av_assert0(0);
  592. }
  593. static int submit_encode_frame(OutputFile *of, OutputStream *ost,
  594. AVFrame *frame)
  595. {
  596. Encoder *e = ost->enc;
  597. int ret;
  598. if (ost->sq_idx_encode < 0)
  599. return encode_frame(of, ost, frame);
  600. if (frame) {
  601. ret = av_frame_ref(e->sq_frame, frame);
  602. if (ret < 0)
  603. return ret;
  604. frame = e->sq_frame;
  605. }
  606. ret = sq_send(of->sq_encode, ost->sq_idx_encode,
  607. SQFRAME(frame));
  608. if (ret < 0) {
  609. if (frame)
  610. av_frame_unref(frame);
  611. if (ret != AVERROR_EOF)
  612. return ret;
  613. }
  614. while (1) {
  615. AVFrame *enc_frame = e->sq_frame;
  616. ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
  617. SQFRAME(enc_frame));
  618. if (ret == AVERROR_EOF) {
  619. enc_frame = NULL;
  620. } else if (ret < 0) {
  621. return (ret == AVERROR(EAGAIN)) ? 0 : ret;
  622. }
  623. ret = encode_frame(of, ost, enc_frame);
  624. if (enc_frame)
  625. av_frame_unref(enc_frame);
  626. if (ret < 0) {
  627. if (ret == AVERROR_EOF)
  628. close_output_stream(ost);
  629. return ret;
  630. }
  631. }
  632. }
  633. static int do_audio_out(OutputFile *of, OutputStream *ost,
  634. AVFrame *frame)
  635. {
  636. AVCodecContext *enc = ost->enc_ctx;
  637. int ret;
  638. if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
  639. enc->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
  640. av_log(ost, AV_LOG_ERROR,
  641. "Audio channel count changed and encoder does not support parameter changes\n");
  642. return 0;
  643. }
  644. if (!check_recording_time(ost, frame->pts, frame->time_base))
  645. return 0;
  646. ret = submit_encode_frame(of, ost, frame);
  647. return (ret < 0 && ret != AVERROR_EOF) ? ret : 0;
  648. }
  649. static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
  650. AVRational tb, const AVFrame *in_picture)
  651. {
  652. double pts_time;
  653. if (kf->ref_pts == AV_NOPTS_VALUE)
  654. kf->ref_pts = in_picture->pts;
  655. pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb);
  656. if (kf->index < kf->nb_pts &&
  657. av_compare_ts(in_picture->pts, tb, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
  658. kf->index++;
  659. goto force_keyframe;
  660. } else if (kf->pexpr) {
  661. double res;
  662. kf->expr_const_values[FKF_T] = pts_time;
  663. res = av_expr_eval(kf->pexpr,
  664. kf->expr_const_values, NULL);
  665. av_log(logctx, AV_LOG_TRACE,
  666. "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
  667. kf->expr_const_values[FKF_N],
  668. kf->expr_const_values[FKF_N_FORCED],
  669. kf->expr_const_values[FKF_PREV_FORCED_N],
  670. kf->expr_const_values[FKF_T],
  671. kf->expr_const_values[FKF_PREV_FORCED_T],
  672. res);
  673. kf->expr_const_values[FKF_N] += 1;
  674. if (res) {
  675. kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
  676. kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
  677. kf->expr_const_values[FKF_N_FORCED] += 1;
  678. goto force_keyframe;
  679. }
  680. } else if (kf->type == KF_FORCE_SOURCE && (in_picture->flags & AV_FRAME_FLAG_KEY)) {
  681. goto force_keyframe;
  682. }
  683. return AV_PICTURE_TYPE_NONE;
  684. force_keyframe:
  685. av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
  686. return AV_PICTURE_TYPE_I;
  687. }
  688. /* May modify/reset frame */
  689. static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *in_picture)
  690. {
  691. int ret;
  692. AVCodecContext *enc = ost->enc_ctx;
  693. if (!check_recording_time(ost, in_picture->pts, ost->enc_ctx->time_base))
  694. return 0;
  695. in_picture->quality = enc->global_quality;
  696. in_picture->pict_type = forced_kf_apply(ost, &ost->kf, enc->time_base, in_picture);
  697. #if FFMPEG_OPT_TOP
  698. if (ost->top_field_first >= 0) {
  699. in_picture->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
  700. in_picture->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * (!!ost->top_field_first);
  701. }
  702. #endif
  703. ret = submit_encode_frame(of, ost, in_picture);
  704. return (ret == AVERROR_EOF) ? 0 : ret;
  705. }
  706. int enc_frame(OutputStream *ost, AVFrame *frame)
  707. {
  708. OutputFile *of = output_files[ost->file_index];
  709. int ret;
  710. ret = enc_open(ost, frame);
  711. if (ret < 0)
  712. return ret;
  713. return ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ?
  714. do_video_out(of, ost, frame) : do_audio_out(of, ost, frame);
  715. }
  716. int enc_flush(void)
  717. {
  718. int ret;
  719. for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
  720. OutputFile *of = output_files[ost->file_index];
  721. if (ost->sq_idx_encode >= 0)
  722. sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
  723. }
  724. for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
  725. Encoder *e = ost->enc;
  726. AVCodecContext *enc = ost->enc_ctx;
  727. OutputFile *of = output_files[ost->file_index];
  728. if (!enc || !e->opened ||
  729. (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO))
  730. continue;
  731. ret = submit_encode_frame(of, ost, NULL);
  732. if (ret != AVERROR_EOF)
  733. return ret;
  734. }
  735. return 0;
  736. }