ffmpeg_enc.c 31 KB

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