encode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * generic encoding-related code
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/attributes.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/frame.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/internal.h"
  25. #include "libavutil/samplefmt.h"
  26. #include "avcodec.h"
  27. #include "frame_thread_encoder.h"
  28. #include "internal.h"
  29. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  30. {
  31. if (avpkt->size < 0) {
  32. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  33. return AVERROR(EINVAL);
  34. }
  35. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  36. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  37. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  38. return AVERROR(EINVAL);
  39. }
  40. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  41. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  42. if (!avpkt->data || avpkt->size < size) {
  43. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  44. avpkt->data = avctx->internal->byte_buffer;
  45. avpkt->size = avctx->internal->byte_buffer_size;
  46. }
  47. }
  48. if (avpkt->data) {
  49. AVBufferRef *buf = avpkt->buf;
  50. if (avpkt->size < size) {
  51. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  52. return AVERROR(EINVAL);
  53. }
  54. av_init_packet(avpkt);
  55. avpkt->buf = buf;
  56. avpkt->size = size;
  57. return 0;
  58. } else {
  59. int ret = av_new_packet(avpkt, size);
  60. if (ret < 0)
  61. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  62. return ret;
  63. }
  64. }
  65. int ff_alloc_packet(AVPacket *avpkt, int size)
  66. {
  67. return ff_alloc_packet2(NULL, avpkt, size, 0);
  68. }
  69. /**
  70. * Pad last frame with silence.
  71. */
  72. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  73. {
  74. AVFrame *frame = NULL;
  75. int ret;
  76. if (!(frame = av_frame_alloc()))
  77. return AVERROR(ENOMEM);
  78. frame->format = src->format;
  79. frame->channel_layout = src->channel_layout;
  80. frame->channels = src->channels;
  81. frame->nb_samples = s->frame_size;
  82. ret = av_frame_get_buffer(frame, 32);
  83. if (ret < 0)
  84. goto fail;
  85. ret = av_frame_copy_props(frame, src);
  86. if (ret < 0)
  87. goto fail;
  88. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  89. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  90. goto fail;
  91. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  92. frame->nb_samples - src->nb_samples,
  93. s->channels, s->sample_fmt)) < 0)
  94. goto fail;
  95. *dst = frame;
  96. return 0;
  97. fail:
  98. av_frame_free(&frame);
  99. return ret;
  100. }
  101. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  102. AVPacket *avpkt,
  103. const AVFrame *frame,
  104. int *got_packet_ptr)
  105. {
  106. AVFrame *extended_frame = NULL;
  107. AVFrame *padded_frame = NULL;
  108. int ret;
  109. AVPacket user_pkt = *avpkt;
  110. int needs_realloc = !user_pkt.data;
  111. *got_packet_ptr = 0;
  112. if (!avctx->codec->encode2) {
  113. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  114. return AVERROR(ENOSYS);
  115. }
  116. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  117. av_packet_unref(avpkt);
  118. return 0;
  119. }
  120. /* ensure that extended_data is properly set */
  121. if (frame && !frame->extended_data) {
  122. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  123. avctx->channels > AV_NUM_DATA_POINTERS) {
  124. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  125. "with more than %d channels, but extended_data is not set.\n",
  126. AV_NUM_DATA_POINTERS);
  127. return AVERROR(EINVAL);
  128. }
  129. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  130. extended_frame = av_frame_alloc();
  131. if (!extended_frame)
  132. return AVERROR(ENOMEM);
  133. memcpy(extended_frame, frame, sizeof(AVFrame));
  134. extended_frame->extended_data = extended_frame->data;
  135. frame = extended_frame;
  136. }
  137. /* extract audio service type metadata */
  138. if (frame) {
  139. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  140. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  141. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  142. }
  143. /* check for valid frame size */
  144. if (frame) {
  145. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  146. if (frame->nb_samples > avctx->frame_size) {
  147. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  148. ret = AVERROR(EINVAL);
  149. goto end;
  150. }
  151. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  152. if (frame->nb_samples < avctx->frame_size &&
  153. !avctx->internal->last_audio_frame) {
  154. ret = pad_last_frame(avctx, &padded_frame, frame);
  155. if (ret < 0)
  156. goto end;
  157. frame = padded_frame;
  158. avctx->internal->last_audio_frame = 1;
  159. }
  160. if (frame->nb_samples != avctx->frame_size) {
  161. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  162. ret = AVERROR(EINVAL);
  163. goto end;
  164. }
  165. }
  166. }
  167. av_assert0(avctx->codec->encode2);
  168. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  169. if (!ret) {
  170. if (*got_packet_ptr) {
  171. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  172. if (avpkt->pts == AV_NOPTS_VALUE)
  173. avpkt->pts = frame->pts;
  174. if (!avpkt->duration)
  175. avpkt->duration = ff_samples_to_time_base(avctx,
  176. frame->nb_samples);
  177. }
  178. avpkt->dts = avpkt->pts;
  179. } else {
  180. avpkt->size = 0;
  181. }
  182. }
  183. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  184. needs_realloc = 0;
  185. if (user_pkt.data) {
  186. if (user_pkt.size >= avpkt->size) {
  187. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  188. } else {
  189. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  190. avpkt->size = user_pkt.size;
  191. ret = -1;
  192. }
  193. avpkt->buf = user_pkt.buf;
  194. avpkt->data = user_pkt.data;
  195. } else if (!avpkt->buf) {
  196. ret = av_packet_make_refcounted(avpkt);
  197. if (ret < 0)
  198. goto end;
  199. }
  200. }
  201. if (!ret) {
  202. if (needs_realloc && avpkt->data) {
  203. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  204. if (ret >= 0)
  205. avpkt->data = avpkt->buf->data;
  206. }
  207. if (frame)
  208. avctx->frame_number++;
  209. }
  210. if (ret < 0 || !*got_packet_ptr) {
  211. av_packet_unref(avpkt);
  212. goto end;
  213. }
  214. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  215. * this needs to be moved to the encoders, but for now we can do it
  216. * here to simplify things */
  217. avpkt->flags |= AV_PKT_FLAG_KEY;
  218. end:
  219. av_frame_free(&padded_frame);
  220. av_free(extended_frame);
  221. return ret;
  222. }
  223. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  224. AVPacket *avpkt,
  225. const AVFrame *frame,
  226. int *got_packet_ptr)
  227. {
  228. int ret;
  229. AVPacket user_pkt = *avpkt;
  230. int needs_realloc = !user_pkt.data;
  231. *got_packet_ptr = 0;
  232. if (!avctx->codec->encode2) {
  233. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  234. return AVERROR(ENOSYS);
  235. }
  236. if(CONFIG_FRAME_THREAD_ENCODER &&
  237. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  238. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  239. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  240. avctx->stats_out[0] = '\0';
  241. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  242. av_packet_unref(avpkt);
  243. return 0;
  244. }
  245. if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
  246. return AVERROR(EINVAL);
  247. if (frame && frame->format == AV_PIX_FMT_NONE)
  248. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  249. if (frame && (frame->width == 0 || frame->height == 0))
  250. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  251. av_assert0(avctx->codec->encode2);
  252. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  253. av_assert0(ret <= 0);
  254. emms_c();
  255. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  256. needs_realloc = 0;
  257. if (user_pkt.data) {
  258. if (user_pkt.size >= avpkt->size) {
  259. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  260. } else {
  261. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  262. avpkt->size = user_pkt.size;
  263. ret = -1;
  264. }
  265. avpkt->buf = user_pkt.buf;
  266. avpkt->data = user_pkt.data;
  267. } else if (!avpkt->buf) {
  268. ret = av_packet_make_refcounted(avpkt);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. }
  273. if (!ret) {
  274. if (!*got_packet_ptr)
  275. avpkt->size = 0;
  276. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  277. avpkt->pts = avpkt->dts = frame->pts;
  278. if (needs_realloc && avpkt->data) {
  279. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  280. if (ret >= 0)
  281. avpkt->data = avpkt->buf->data;
  282. }
  283. if (frame)
  284. avctx->frame_number++;
  285. }
  286. if (ret < 0 || !*got_packet_ptr)
  287. av_packet_unref(avpkt);
  288. return ret;
  289. }
  290. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  291. const AVSubtitle *sub)
  292. {
  293. int ret;
  294. if (sub->start_display_time) {
  295. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  296. return -1;
  297. }
  298. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  299. avctx->frame_number++;
  300. return ret;
  301. }
  302. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  303. {
  304. int ret;
  305. *got_packet = 0;
  306. av_packet_unref(avctx->internal->buffer_pkt);
  307. avctx->internal->buffer_pkt_valid = 0;
  308. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  309. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  310. frame, got_packet);
  311. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  312. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  313. frame, got_packet);
  314. } else {
  315. ret = AVERROR(EINVAL);
  316. }
  317. if (ret >= 0 && *got_packet) {
  318. // Encoders must always return ref-counted buffers.
  319. // Side-data only packets have no data and can be not ref-counted.
  320. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  321. avctx->internal->buffer_pkt_valid = 1;
  322. ret = 0;
  323. } else {
  324. av_packet_unref(avctx->internal->buffer_pkt);
  325. }
  326. return ret;
  327. }
  328. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  329. {
  330. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  331. return AVERROR(EINVAL);
  332. if (avctx->internal->draining)
  333. return AVERROR_EOF;
  334. if (!frame) {
  335. avctx->internal->draining = 1;
  336. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  337. return 0;
  338. }
  339. if (avctx->codec->send_frame)
  340. return avctx->codec->send_frame(avctx, frame);
  341. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  342. // 1. if the AVFrame is not refcounted, the copying will be much more
  343. // expensive than copying the packet data
  344. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  345. // needed
  346. if (avctx->internal->buffer_pkt_valid)
  347. return AVERROR(EAGAIN);
  348. return do_encode(avctx, frame, &(int){0});
  349. }
  350. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  351. {
  352. av_packet_unref(avpkt);
  353. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  354. return AVERROR(EINVAL);
  355. if (avctx->codec->receive_packet) {
  356. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  357. return AVERROR_EOF;
  358. return avctx->codec->receive_packet(avctx, avpkt);
  359. }
  360. // Emulation via old API.
  361. if (!avctx->internal->buffer_pkt_valid) {
  362. int got_packet;
  363. int ret;
  364. if (!avctx->internal->draining)
  365. return AVERROR(EAGAIN);
  366. ret = do_encode(avctx, NULL, &got_packet);
  367. if (ret < 0)
  368. return ret;
  369. if (ret >= 0 && !got_packet)
  370. return AVERROR_EOF;
  371. }
  372. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  373. avctx->internal->buffer_pkt_valid = 0;
  374. return 0;
  375. }