You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
14KB

  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 "encode.h"
  28. #include "frame_thread_encoder.h"
  29. #include "internal.h"
  30. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  31. {
  32. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  33. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  34. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  35. return AVERROR(EINVAL);
  36. }
  37. av_assert0(!avpkt->data);
  38. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  39. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  40. avpkt->data = avctx->internal->byte_buffer;
  41. avpkt->size = size;
  42. }
  43. if (!avpkt->data) {
  44. int ret = av_new_packet(avpkt, size);
  45. if (ret < 0)
  46. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  47. return ret;
  48. }
  49. return 0;
  50. }
  51. /**
  52. * Pad last frame with silence.
  53. */
  54. static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
  55. {
  56. int ret;
  57. frame->format = src->format;
  58. frame->channel_layout = src->channel_layout;
  59. frame->channels = src->channels;
  60. frame->nb_samples = s->frame_size;
  61. ret = av_frame_get_buffer(frame, 0);
  62. if (ret < 0)
  63. goto fail;
  64. ret = av_frame_copy_props(frame, src);
  65. if (ret < 0)
  66. goto fail;
  67. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  68. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  69. goto fail;
  70. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  71. frame->nb_samples - src->nb_samples,
  72. s->channels, s->sample_fmt)) < 0)
  73. goto fail;
  74. return 0;
  75. fail:
  76. av_frame_unref(frame);
  77. return ret;
  78. }
  79. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  80. const AVSubtitle *sub)
  81. {
  82. int ret;
  83. if (sub->start_display_time) {
  84. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  85. return -1;
  86. }
  87. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  88. avctx->frame_number++;
  89. return ret;
  90. }
  91. int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
  92. {
  93. AVCodecInternal *avci = avctx->internal;
  94. if (avci->draining)
  95. return AVERROR_EOF;
  96. if (!avci->buffer_frame->buf[0])
  97. return AVERROR(EAGAIN);
  98. av_frame_move_ref(frame, avci->buffer_frame);
  99. return 0;
  100. }
  101. static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
  102. {
  103. AVCodecInternal *avci = avctx->internal;
  104. EncodeSimpleContext *es = &avci->es;
  105. AVFrame *frame = es->in_frame;
  106. int got_packet;
  107. int ret;
  108. if (avci->draining_done)
  109. return AVERROR_EOF;
  110. if (!frame->buf[0] && !avci->draining) {
  111. av_frame_unref(frame);
  112. ret = ff_encode_get_frame(avctx, frame);
  113. if (ret < 0 && ret != AVERROR_EOF)
  114. return ret;
  115. }
  116. if (!frame->buf[0]) {
  117. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
  118. (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
  119. return AVERROR_EOF;
  120. // Flushing is signaled with a NULL frame
  121. frame = NULL;
  122. }
  123. got_packet = 0;
  124. av_assert0(avctx->codec->encode2);
  125. if (CONFIG_FRAME_THREAD_ENCODER &&
  126. avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
  127. /* This might modify frame, but it doesn't matter, because
  128. * the frame properties used below are not used for video
  129. * (due to the delay inherent in frame threaded encoding, it makes
  130. * no sense to use the properties of the current frame anyway). */
  131. ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
  132. else {
  133. ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
  134. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
  135. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  136. avpkt->pts = avpkt->dts = frame->pts;
  137. }
  138. av_assert0(ret <= 0);
  139. emms_c();
  140. if (!ret && got_packet) {
  141. if (avpkt->data) {
  142. ret = av_packet_make_refcounted(avpkt);
  143. if (ret < 0)
  144. goto end;
  145. }
  146. if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  147. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  148. if (avpkt->pts == AV_NOPTS_VALUE)
  149. avpkt->pts = frame->pts;
  150. if (!avpkt->duration)
  151. avpkt->duration = ff_samples_to_time_base(avctx,
  152. frame->nb_samples);
  153. }
  154. }
  155. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  156. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  157. * this needs to be moved to the encoders, but for now we can do it
  158. * here to simplify things */
  159. avpkt->flags |= AV_PKT_FLAG_KEY;
  160. avpkt->dts = avpkt->pts;
  161. }
  162. }
  163. if (avci->draining && !got_packet)
  164. avci->draining_done = 1;
  165. end:
  166. if (ret < 0 || !got_packet)
  167. av_packet_unref(avpkt);
  168. if (frame) {
  169. if (!ret)
  170. avctx->frame_number++;
  171. av_frame_unref(frame);
  172. }
  173. if (got_packet)
  174. // Encoders must always return ref-counted buffers.
  175. // Side-data only packets have no data and can be not ref-counted.
  176. av_assert0(!avpkt->data || avpkt->buf);
  177. return ret;
  178. }
  179. static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  180. {
  181. int ret;
  182. while (!avpkt->data && !avpkt->side_data) {
  183. ret = encode_simple_internal(avctx, avpkt);
  184. if (ret < 0)
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
  190. {
  191. AVCodecInternal *avci = avctx->internal;
  192. int ret;
  193. if (avci->draining_done)
  194. return AVERROR_EOF;
  195. av_assert0(!avpkt->data && !avpkt->side_data);
  196. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  197. if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  198. avctx->stats_out[0] = '\0';
  199. if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
  200. return AVERROR(EINVAL);
  201. }
  202. if (avctx->codec->receive_packet) {
  203. ret = avctx->codec->receive_packet(avctx, avpkt);
  204. if (ret < 0)
  205. av_packet_unref(avpkt);
  206. else
  207. // Encoders must always return ref-counted buffers.
  208. // Side-data only packets have no data and can be not ref-counted.
  209. av_assert0(!avpkt->data || avpkt->buf);
  210. } else
  211. ret = encode_simple_receive_packet(avctx, avpkt);
  212. if (ret == AVERROR_EOF)
  213. avci->draining_done = 1;
  214. return ret;
  215. }
  216. static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
  217. {
  218. AVCodecInternal *avci = avctx->internal;
  219. AVFrame *dst = avci->buffer_frame;
  220. int ret;
  221. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  222. /* extract audio service type metadata */
  223. AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  224. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  225. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  226. /* check for valid frame size */
  227. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  228. if (src->nb_samples > avctx->frame_size) {
  229. av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
  230. return AVERROR(EINVAL);
  231. }
  232. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  233. /* if we already got an undersized frame, that must have been the last */
  234. if (avctx->internal->last_audio_frame) {
  235. av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
  236. return AVERROR(EINVAL);
  237. }
  238. if (src->nb_samples < avctx->frame_size) {
  239. ret = pad_last_frame(avctx, dst, src);
  240. if (ret < 0)
  241. return ret;
  242. avctx->internal->last_audio_frame = 1;
  243. } else if (src->nb_samples > avctx->frame_size) {
  244. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
  245. return AVERROR(EINVAL);
  246. }
  247. }
  248. }
  249. if (!dst->data[0]) {
  250. ret = av_frame_ref(dst, src);
  251. if (ret < 0)
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  257. {
  258. AVCodecInternal *avci = avctx->internal;
  259. int ret;
  260. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  261. return AVERROR(EINVAL);
  262. if (avci->draining)
  263. return AVERROR_EOF;
  264. if (avci->buffer_frame->data[0])
  265. return AVERROR(EAGAIN);
  266. if (!frame) {
  267. avci->draining = 1;
  268. } else {
  269. ret = encode_send_frame_internal(avctx, frame);
  270. if (ret < 0)
  271. return ret;
  272. }
  273. if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
  274. ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
  275. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  281. {
  282. AVCodecInternal *avci = avctx->internal;
  283. int ret;
  284. av_packet_unref(avpkt);
  285. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  286. return AVERROR(EINVAL);
  287. if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
  288. av_packet_move_ref(avpkt, avci->buffer_pkt);
  289. } else {
  290. ret = encode_receive_packet_internal(avctx, avpkt);
  291. if (ret < 0)
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. #if FF_API_OLD_ENCDEC
  297. static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
  298. int *got_packet, const AVFrame *frame)
  299. {
  300. AVCodecInternal *avci = avctx->internal;
  301. AVPacket user_pkt;
  302. int ret;
  303. *got_packet = 0;
  304. if (frame && avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  305. if (frame->format == AV_PIX_FMT_NONE)
  306. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  307. if (frame->width == 0 || frame->height == 0)
  308. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  309. }
  310. ret = avcodec_send_frame(avctx, frame);
  311. if (ret == AVERROR_EOF)
  312. ret = 0;
  313. else if (ret == AVERROR(EAGAIN)) {
  314. /* we fully drain all the output in each encode call, so this should not
  315. * ever happen */
  316. return AVERROR_BUG;
  317. } else if (ret < 0)
  318. return ret;
  319. av_packet_move_ref(&user_pkt, avpkt);
  320. while (ret >= 0) {
  321. ret = avcodec_receive_packet(avctx, avpkt);
  322. if (ret < 0) {
  323. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  324. ret = 0;
  325. goto finish;
  326. }
  327. if (avpkt != avci->compat_encode_packet) {
  328. if (avpkt->data && user_pkt.data) {
  329. if (user_pkt.size >= avpkt->size) {
  330. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  331. av_buffer_unref(&avpkt->buf);
  332. avpkt->buf = user_pkt.buf;
  333. avpkt->data = user_pkt.data;
  334. av_init_packet(&user_pkt);
  335. } else {
  336. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  337. av_packet_unref(avpkt);
  338. ret = AVERROR(EINVAL);
  339. goto finish;
  340. }
  341. }
  342. *got_packet = 1;
  343. avpkt = avci->compat_encode_packet;
  344. } else {
  345. if (!avci->compat_decode_warned) {
  346. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* "
  347. "API cannot return all the packets for this encoder. "
  348. "Some packets will be dropped. Update your code to the "
  349. "new encoding API to fix this.\n");
  350. avci->compat_decode_warned = 1;
  351. av_packet_unref(avpkt);
  352. }
  353. }
  354. if (avci->draining)
  355. break;
  356. }
  357. finish:
  358. if (ret < 0)
  359. av_packet_unref(&user_pkt);
  360. return ret;
  361. }
  362. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  363. AVPacket *avpkt,
  364. const AVFrame *frame,
  365. int *got_packet_ptr)
  366. {
  367. int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
  368. if (ret < 0)
  369. av_packet_unref(avpkt);
  370. return ret;
  371. }
  372. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  373. AVPacket *avpkt,
  374. const AVFrame *frame,
  375. int *got_packet_ptr)
  376. {
  377. int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
  378. if (ret < 0)
  379. av_packet_unref(avpkt);
  380. return ret;
  381. }
  382. #endif