ffmpeg_enc.c 30 KB

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