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.

459 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. ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
  128. else {
  129. ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
  130. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
  131. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  132. avpkt->pts = avpkt->dts = frame->pts;
  133. }
  134. av_assert0(ret <= 0);
  135. emms_c();
  136. if (!ret && got_packet) {
  137. if (avpkt->data) {
  138. ret = av_packet_make_refcounted(avpkt);
  139. if (ret < 0)
  140. goto end;
  141. }
  142. if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  143. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  144. if (avpkt->pts == AV_NOPTS_VALUE)
  145. avpkt->pts = frame->pts;
  146. if (!avpkt->duration)
  147. avpkt->duration = ff_samples_to_time_base(avctx,
  148. frame->nb_samples);
  149. }
  150. }
  151. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  152. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  153. * this needs to be moved to the encoders, but for now we can do it
  154. * here to simplify things */
  155. avpkt->flags |= AV_PKT_FLAG_KEY;
  156. avpkt->dts = avpkt->pts;
  157. }
  158. }
  159. if (avci->draining && !got_packet)
  160. avci->draining_done = 1;
  161. end:
  162. if (ret < 0 || !got_packet)
  163. av_packet_unref(avpkt);
  164. if (frame) {
  165. if (!ret)
  166. avctx->frame_number++;
  167. av_frame_unref(frame);
  168. }
  169. if (got_packet)
  170. // Encoders must always return ref-counted buffers.
  171. // Side-data only packets have no data and can be not ref-counted.
  172. av_assert0(!avpkt->data || avpkt->buf);
  173. return ret;
  174. }
  175. static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  176. {
  177. int ret;
  178. while (!avpkt->data && !avpkt->side_data) {
  179. ret = encode_simple_internal(avctx, avpkt);
  180. if (ret < 0)
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
  186. {
  187. AVCodecInternal *avci = avctx->internal;
  188. int ret;
  189. if (avci->draining_done)
  190. return AVERROR_EOF;
  191. av_assert0(!avpkt->data && !avpkt->side_data);
  192. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  193. if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  194. avctx->stats_out[0] = '\0';
  195. if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
  196. return AVERROR(EINVAL);
  197. }
  198. if (avctx->codec->receive_packet) {
  199. ret = avctx->codec->receive_packet(avctx, avpkt);
  200. if (ret < 0)
  201. av_packet_unref(avpkt);
  202. else
  203. // Encoders must always return ref-counted buffers.
  204. // Side-data only packets have no data and can be not ref-counted.
  205. av_assert0(!avpkt->data || avpkt->buf);
  206. } else
  207. ret = encode_simple_receive_packet(avctx, avpkt);
  208. if (ret == AVERROR_EOF)
  209. avci->draining_done = 1;
  210. return ret;
  211. }
  212. static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
  213. {
  214. AVCodecInternal *avci = avctx->internal;
  215. AVFrame *dst = avci->buffer_frame;
  216. int ret;
  217. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  218. /* extract audio service type metadata */
  219. AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  220. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  221. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  222. /* check for valid frame size */
  223. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  224. if (src->nb_samples > avctx->frame_size) {
  225. av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
  226. return AVERROR(EINVAL);
  227. }
  228. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  229. /* if we already got an undersized frame, that must have been the last */
  230. if (avctx->internal->last_audio_frame) {
  231. av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
  232. return AVERROR(EINVAL);
  233. }
  234. if (src->nb_samples < avctx->frame_size) {
  235. ret = pad_last_frame(avctx, dst, src);
  236. if (ret < 0)
  237. return ret;
  238. avctx->internal->last_audio_frame = 1;
  239. } else if (src->nb_samples > avctx->frame_size) {
  240. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
  241. return AVERROR(EINVAL);
  242. }
  243. }
  244. }
  245. if (!dst->data[0]) {
  246. ret = av_frame_ref(dst, src);
  247. if (ret < 0)
  248. return ret;
  249. }
  250. return 0;
  251. }
  252. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  253. {
  254. AVCodecInternal *avci = avctx->internal;
  255. int ret;
  256. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  257. return AVERROR(EINVAL);
  258. if (avci->draining)
  259. return AVERROR_EOF;
  260. if (avci->buffer_frame->data[0])
  261. return AVERROR(EAGAIN);
  262. if (!frame) {
  263. avci->draining = 1;
  264. } else {
  265. ret = encode_send_frame_internal(avctx, frame);
  266. if (ret < 0)
  267. return ret;
  268. }
  269. if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
  270. ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
  271. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  272. return ret;
  273. }
  274. return 0;
  275. }
  276. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  277. {
  278. AVCodecInternal *avci = avctx->internal;
  279. int ret;
  280. av_packet_unref(avpkt);
  281. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  282. return AVERROR(EINVAL);
  283. if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
  284. av_packet_move_ref(avpkt, avci->buffer_pkt);
  285. } else {
  286. ret = encode_receive_packet_internal(avctx, avpkt);
  287. if (ret < 0)
  288. return ret;
  289. }
  290. return 0;
  291. }
  292. static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
  293. int *got_packet, const AVFrame *frame)
  294. {
  295. AVCodecInternal *avci = avctx->internal;
  296. AVPacket user_pkt;
  297. int ret;
  298. *got_packet = 0;
  299. if (frame && avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  300. if (frame->format == AV_PIX_FMT_NONE)
  301. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  302. if (frame->width == 0 || frame->height == 0)
  303. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  304. }
  305. ret = avcodec_send_frame(avctx, frame);
  306. if (ret == AVERROR_EOF)
  307. ret = 0;
  308. else if (ret == AVERROR(EAGAIN)) {
  309. /* we fully drain all the output in each encode call, so this should not
  310. * ever happen */
  311. return AVERROR_BUG;
  312. } else if (ret < 0)
  313. return ret;
  314. av_packet_move_ref(&user_pkt, avpkt);
  315. while (ret >= 0) {
  316. ret = avcodec_receive_packet(avctx, avpkt);
  317. if (ret < 0) {
  318. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  319. ret = 0;
  320. goto finish;
  321. }
  322. if (avpkt != avci->compat_encode_packet) {
  323. if (avpkt->data && user_pkt.data) {
  324. if (user_pkt.size >= avpkt->size) {
  325. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  326. av_buffer_unref(&avpkt->buf);
  327. avpkt->buf = user_pkt.buf;
  328. avpkt->data = user_pkt.data;
  329. av_init_packet(&user_pkt);
  330. } else {
  331. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  332. av_packet_unref(avpkt);
  333. ret = AVERROR(EINVAL);
  334. goto finish;
  335. }
  336. }
  337. *got_packet = 1;
  338. avpkt = avci->compat_encode_packet;
  339. } else {
  340. if (!avci->compat_decode_warned) {
  341. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* "
  342. "API cannot return all the packets for this encoder. "
  343. "Some packets will be dropped. Update your code to the "
  344. "new encoding API to fix this.\n");
  345. avci->compat_decode_warned = 1;
  346. av_packet_unref(avpkt);
  347. }
  348. }
  349. if (avci->draining)
  350. break;
  351. }
  352. finish:
  353. if (ret < 0)
  354. av_packet_unref(&user_pkt);
  355. return ret;
  356. }
  357. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  358. AVPacket *avpkt,
  359. const AVFrame *frame,
  360. int *got_packet_ptr)
  361. {
  362. int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
  363. if (ret < 0)
  364. av_packet_unref(avpkt);
  365. return ret;
  366. }
  367. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  368. AVPacket *avpkt,
  369. const AVFrame *frame,
  370. int *got_packet_ptr)
  371. {
  372. int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
  373. if (ret < 0)
  374. av_packet_unref(avpkt);
  375. return ret;
  376. }