ffmpeg_enc.c 32 KB

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