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.

461 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 if (!avpkt->buf) {
  197. AVPacket tmp = { 0 };
  198. ret = av_packet_ref(&tmp, avpkt);
  199. if (ret < 0)
  200. return ret;
  201. av_packet_unref(avpkt);
  202. *avpkt = tmp;
  203. }
  204. }
  205. if (!ret) {
  206. if (needs_realloc && avpkt->data) {
  207. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  208. if (ret >= 0)
  209. avpkt->data = avpkt->buf->data;
  210. }
  211. avctx->frame_number++;
  212. }
  213. if (ret < 0 || !*got_packet_ptr) {
  214. av_packet_unref(avpkt);
  215. av_init_packet(avpkt);
  216. goto end;
  217. }
  218. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  219. * this needs to be moved to the encoders, but for now we can do it
  220. * here to simplify things */
  221. avpkt->flags |= AV_PKT_FLAG_KEY;
  222. end:
  223. av_frame_free(&padded_frame);
  224. av_free(extended_frame);
  225. #if FF_API_AUDIOENC_DELAY
  226. avctx->delay = avctx->initial_padding;
  227. #endif
  228. return ret;
  229. }
  230. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  231. AVPacket *avpkt,
  232. const AVFrame *frame,
  233. int *got_packet_ptr)
  234. {
  235. int ret;
  236. AVPacket user_pkt = *avpkt;
  237. int needs_realloc = !user_pkt.data;
  238. *got_packet_ptr = 0;
  239. if (!avctx->codec->encode2) {
  240. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  241. return AVERROR(ENOSYS);
  242. }
  243. if(CONFIG_FRAME_THREAD_ENCODER &&
  244. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  245. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  246. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  247. avctx->stats_out[0] = '\0';
  248. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  249. av_packet_unref(avpkt);
  250. av_init_packet(avpkt);
  251. avpkt->size = 0;
  252. return 0;
  253. }
  254. if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
  255. return AVERROR(EINVAL);
  256. if (frame && frame->format == AV_PIX_FMT_NONE)
  257. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  258. if (frame && (frame->width == 0 || frame->height == 0))
  259. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  260. av_assert0(avctx->codec->encode2);
  261. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  262. av_assert0(ret <= 0);
  263. emms_c();
  264. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  265. needs_realloc = 0;
  266. if (user_pkt.data) {
  267. if (user_pkt.size >= avpkt->size) {
  268. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  269. } else {
  270. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  271. avpkt->size = user_pkt.size;
  272. ret = -1;
  273. }
  274. avpkt->buf = user_pkt.buf;
  275. avpkt->data = user_pkt.data;
  276. } else if (!avpkt->buf) {
  277. AVPacket tmp = { 0 };
  278. ret = av_packet_ref(&tmp, avpkt);
  279. if (ret < 0)
  280. return ret;
  281. av_packet_unref(avpkt);
  282. *avpkt = tmp;
  283. }
  284. }
  285. if (!ret) {
  286. if (!*got_packet_ptr)
  287. avpkt->size = 0;
  288. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  289. avpkt->pts = avpkt->dts = frame->pts;
  290. if (needs_realloc && avpkt->data) {
  291. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  292. if (ret >= 0)
  293. avpkt->data = avpkt->buf->data;
  294. }
  295. avctx->frame_number++;
  296. }
  297. if (ret < 0 || !*got_packet_ptr)
  298. av_packet_unref(avpkt);
  299. return ret;
  300. }
  301. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  302. const AVSubtitle *sub)
  303. {
  304. int ret;
  305. if (sub->start_display_time) {
  306. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  307. return -1;
  308. }
  309. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  310. avctx->frame_number++;
  311. return ret;
  312. }
  313. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  314. {
  315. int ret;
  316. *got_packet = 0;
  317. av_packet_unref(avctx->internal->buffer_pkt);
  318. avctx->internal->buffer_pkt_valid = 0;
  319. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  320. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  321. frame, got_packet);
  322. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  323. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  324. frame, got_packet);
  325. } else {
  326. ret = AVERROR(EINVAL);
  327. }
  328. if (ret >= 0 && *got_packet) {
  329. // Encoders must always return ref-counted buffers.
  330. // Side-data only packets have no data and can be not ref-counted.
  331. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  332. avctx->internal->buffer_pkt_valid = 1;
  333. ret = 0;
  334. } else {
  335. av_packet_unref(avctx->internal->buffer_pkt);
  336. }
  337. return ret;
  338. }
  339. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  340. {
  341. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  342. return AVERROR(EINVAL);
  343. if (avctx->internal->draining)
  344. return AVERROR_EOF;
  345. if (!frame) {
  346. avctx->internal->draining = 1;
  347. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  348. return 0;
  349. }
  350. if (avctx->codec->send_frame)
  351. return avctx->codec->send_frame(avctx, frame);
  352. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  353. // 1. if the AVFrame is not refcounted, the copying will be much more
  354. // expensive than copying the packet data
  355. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  356. // needed
  357. if (avctx->internal->buffer_pkt_valid)
  358. return AVERROR(EAGAIN);
  359. return do_encode(avctx, frame, &(int){0});
  360. }
  361. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  362. {
  363. av_packet_unref(avpkt);
  364. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  365. return AVERROR(EINVAL);
  366. if (avctx->codec->receive_packet) {
  367. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  368. return AVERROR_EOF;
  369. return avctx->codec->receive_packet(avctx, avpkt);
  370. }
  371. // Emulation via old API.
  372. if (!avctx->internal->buffer_pkt_valid) {
  373. int got_packet;
  374. int ret;
  375. if (!avctx->internal->draining)
  376. return AVERROR(EAGAIN);
  377. ret = do_encode(avctx, NULL, &got_packet);
  378. if (ret < 0)
  379. return ret;
  380. if (ret >= 0 && !got_packet)
  381. return AVERROR_EOF;
  382. }
  383. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  384. avctx->internal->buffer_pkt_valid = 0;
  385. return 0;
  386. }