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.

455 lines
15KB

  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. av_init_packet(avpkt);
  119. return 0;
  120. }
  121. /* ensure that extended_data is properly set */
  122. if (frame && !frame->extended_data) {
  123. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  124. avctx->channels > AV_NUM_DATA_POINTERS) {
  125. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  126. "with more than %d channels, but extended_data is not set.\n",
  127. AV_NUM_DATA_POINTERS);
  128. return AVERROR(EINVAL);
  129. }
  130. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  131. extended_frame = av_frame_alloc();
  132. if (!extended_frame)
  133. return AVERROR(ENOMEM);
  134. memcpy(extended_frame, frame, sizeof(AVFrame));
  135. extended_frame->extended_data = extended_frame->data;
  136. frame = extended_frame;
  137. }
  138. /* extract audio service type metadata */
  139. if (frame) {
  140. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  141. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  142. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  143. }
  144. /* check for valid frame size */
  145. if (frame) {
  146. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  147. if (frame->nb_samples > avctx->frame_size) {
  148. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  149. ret = AVERROR(EINVAL);
  150. goto end;
  151. }
  152. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  153. if (frame->nb_samples < avctx->frame_size &&
  154. !avctx->internal->last_audio_frame) {
  155. ret = pad_last_frame(avctx, &padded_frame, frame);
  156. if (ret < 0)
  157. goto end;
  158. frame = padded_frame;
  159. avctx->internal->last_audio_frame = 1;
  160. }
  161. if (frame->nb_samples != avctx->frame_size) {
  162. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  163. ret = AVERROR(EINVAL);
  164. goto end;
  165. }
  166. }
  167. }
  168. av_assert0(avctx->codec->encode2);
  169. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  170. if (!ret) {
  171. if (*got_packet_ptr) {
  172. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  173. if (avpkt->pts == AV_NOPTS_VALUE)
  174. avpkt->pts = frame->pts;
  175. if (!avpkt->duration)
  176. avpkt->duration = ff_samples_to_time_base(avctx,
  177. frame->nb_samples);
  178. }
  179. avpkt->dts = avpkt->pts;
  180. } else {
  181. avpkt->size = 0;
  182. }
  183. }
  184. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  185. needs_realloc = 0;
  186. if (user_pkt.data) {
  187. if (user_pkt.size >= avpkt->size) {
  188. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  189. } else {
  190. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  191. avpkt->size = user_pkt.size;
  192. ret = -1;
  193. }
  194. avpkt->buf = user_pkt.buf;
  195. avpkt->data = user_pkt.data;
  196. } else {
  197. if (av_dup_packet(avpkt) < 0) {
  198. ret = AVERROR(ENOMEM);
  199. }
  200. }
  201. }
  202. if (!ret) {
  203. if (needs_realloc && avpkt->data) {
  204. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  205. if (ret >= 0)
  206. avpkt->data = avpkt->buf->data;
  207. }
  208. avctx->frame_number++;
  209. }
  210. if (ret < 0 || !*got_packet_ptr) {
  211. av_packet_unref(avpkt);
  212. av_init_packet(avpkt);
  213. goto end;
  214. }
  215. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  216. * this needs to be moved to the encoders, but for now we can do it
  217. * here to simplify things */
  218. avpkt->flags |= AV_PKT_FLAG_KEY;
  219. end:
  220. av_frame_free(&padded_frame);
  221. av_free(extended_frame);
  222. #if FF_API_AUDIOENC_DELAY
  223. avctx->delay = avctx->initial_padding;
  224. #endif
  225. return ret;
  226. }
  227. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  228. AVPacket *avpkt,
  229. const AVFrame *frame,
  230. int *got_packet_ptr)
  231. {
  232. int ret;
  233. AVPacket user_pkt = *avpkt;
  234. int needs_realloc = !user_pkt.data;
  235. *got_packet_ptr = 0;
  236. if (!avctx->codec->encode2) {
  237. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  238. return AVERROR(ENOSYS);
  239. }
  240. if(CONFIG_FRAME_THREAD_ENCODER &&
  241. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  242. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  243. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  244. avctx->stats_out[0] = '\0';
  245. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  246. av_packet_unref(avpkt);
  247. av_init_packet(avpkt);
  248. avpkt->size = 0;
  249. return 0;
  250. }
  251. if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
  252. return AVERROR(EINVAL);
  253. if (frame && frame->format == AV_PIX_FMT_NONE)
  254. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  255. if (frame && (frame->width == 0 || frame->height == 0))
  256. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  257. av_assert0(avctx->codec->encode2);
  258. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  259. av_assert0(ret <= 0);
  260. emms_c();
  261. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  262. needs_realloc = 0;
  263. if (user_pkt.data) {
  264. if (user_pkt.size >= avpkt->size) {
  265. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  266. } else {
  267. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  268. avpkt->size = user_pkt.size;
  269. ret = -1;
  270. }
  271. avpkt->buf = user_pkt.buf;
  272. avpkt->data = user_pkt.data;
  273. } else {
  274. if (av_dup_packet(avpkt) < 0) {
  275. ret = AVERROR(ENOMEM);
  276. }
  277. }
  278. }
  279. if (!ret) {
  280. if (!*got_packet_ptr)
  281. avpkt->size = 0;
  282. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  283. avpkt->pts = avpkt->dts = frame->pts;
  284. if (needs_realloc && avpkt->data) {
  285. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  286. if (ret >= 0)
  287. avpkt->data = avpkt->buf->data;
  288. }
  289. avctx->frame_number++;
  290. }
  291. if (ret < 0 || !*got_packet_ptr)
  292. av_packet_unref(avpkt);
  293. return ret;
  294. }
  295. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  296. const AVSubtitle *sub)
  297. {
  298. int ret;
  299. if (sub->start_display_time) {
  300. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  301. return -1;
  302. }
  303. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  304. avctx->frame_number++;
  305. return ret;
  306. }
  307. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  308. {
  309. int ret;
  310. *got_packet = 0;
  311. av_packet_unref(avctx->internal->buffer_pkt);
  312. avctx->internal->buffer_pkt_valid = 0;
  313. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  314. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  315. frame, got_packet);
  316. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  317. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  318. frame, got_packet);
  319. } else {
  320. ret = AVERROR(EINVAL);
  321. }
  322. if (ret >= 0 && *got_packet) {
  323. // Encoders must always return ref-counted buffers.
  324. // Side-data only packets have no data and can be not ref-counted.
  325. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  326. avctx->internal->buffer_pkt_valid = 1;
  327. ret = 0;
  328. } else {
  329. av_packet_unref(avctx->internal->buffer_pkt);
  330. }
  331. return ret;
  332. }
  333. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  334. {
  335. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  336. return AVERROR(EINVAL);
  337. if (avctx->internal->draining)
  338. return AVERROR_EOF;
  339. if (!frame) {
  340. avctx->internal->draining = 1;
  341. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  342. return 0;
  343. }
  344. if (avctx->codec->send_frame)
  345. return avctx->codec->send_frame(avctx, frame);
  346. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  347. // 1. if the AVFrame is not refcounted, the copying will be much more
  348. // expensive than copying the packet data
  349. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  350. // needed
  351. if (avctx->internal->buffer_pkt_valid)
  352. return AVERROR(EAGAIN);
  353. return do_encode(avctx, frame, &(int){0});
  354. }
  355. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  356. {
  357. av_packet_unref(avpkt);
  358. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  359. return AVERROR(EINVAL);
  360. if (avctx->codec->receive_packet) {
  361. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  362. return AVERROR_EOF;
  363. return avctx->codec->receive_packet(avctx, avpkt);
  364. }
  365. // Emulation via old API.
  366. if (!avctx->internal->buffer_pkt_valid) {
  367. int got_packet;
  368. int ret;
  369. if (!avctx->internal->draining)
  370. return AVERROR(EAGAIN);
  371. ret = do_encode(avctx, NULL, &got_packet);
  372. if (ret < 0)
  373. return ret;
  374. if (ret >= 0 && !got_packet)
  375. return AVERROR_EOF;
  376. }
  377. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  378. avctx->internal->buffer_pkt_valid = 0;
  379. return 0;
  380. }