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.

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