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.

460 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, 0);
  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 we already got an undersized frame, that must have been the last */
  153. if (avctx->internal->last_audio_frame) {
  154. av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size);
  155. ret = AVERROR(EINVAL);
  156. goto end;
  157. }
  158. if (frame->nb_samples < avctx->frame_size) {
  159. ret = pad_last_frame(avctx, &padded_frame, frame);
  160. if (ret < 0)
  161. goto end;
  162. frame = padded_frame;
  163. avctx->internal->last_audio_frame = 1;
  164. }
  165. if (frame->nb_samples != avctx->frame_size) {
  166. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  167. ret = AVERROR(EINVAL);
  168. goto end;
  169. }
  170. }
  171. }
  172. av_assert0(avctx->codec->encode2);
  173. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  174. if (!ret) {
  175. if (*got_packet_ptr) {
  176. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  177. if (avpkt->pts == AV_NOPTS_VALUE)
  178. avpkt->pts = frame->pts;
  179. if (!avpkt->duration)
  180. avpkt->duration = ff_samples_to_time_base(avctx,
  181. frame->nb_samples);
  182. }
  183. avpkt->dts = avpkt->pts;
  184. } else {
  185. avpkt->size = 0;
  186. }
  187. }
  188. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  189. needs_realloc = 0;
  190. if (user_pkt.data) {
  191. if (user_pkt.size >= avpkt->size) {
  192. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  193. } else {
  194. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  195. avpkt->size = user_pkt.size;
  196. ret = -1;
  197. }
  198. avpkt->buf = user_pkt.buf;
  199. avpkt->data = user_pkt.data;
  200. } else if (!avpkt->buf) {
  201. ret = av_packet_make_refcounted(avpkt);
  202. if (ret < 0)
  203. goto end;
  204. }
  205. }
  206. if (!ret) {
  207. if (needs_realloc && avpkt->data) {
  208. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  209. if (ret >= 0)
  210. avpkt->data = avpkt->buf->data;
  211. }
  212. if (frame)
  213. avctx->frame_number++;
  214. }
  215. if (ret < 0 || !*got_packet_ptr) {
  216. av_packet_unref(avpkt);
  217. goto end;
  218. }
  219. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  220. * this needs to be moved to the encoders, but for now we can do it
  221. * here to simplify things */
  222. avpkt->flags |= AV_PKT_FLAG_KEY;
  223. end:
  224. av_frame_free(&padded_frame);
  225. av_free(extended_frame);
  226. return ret;
  227. }
  228. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  229. AVPacket *avpkt,
  230. const AVFrame *frame,
  231. int *got_packet_ptr)
  232. {
  233. int ret;
  234. AVPacket user_pkt = *avpkt;
  235. int needs_realloc = !user_pkt.data;
  236. *got_packet_ptr = 0;
  237. if (!avctx->codec->encode2) {
  238. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  239. return AVERROR(ENOSYS);
  240. }
  241. if(CONFIG_FRAME_THREAD_ENCODER &&
  242. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  243. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  244. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  245. avctx->stats_out[0] = '\0';
  246. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  247. av_packet_unref(avpkt);
  248. return 0;
  249. }
  250. if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
  251. return AVERROR(EINVAL);
  252. if (frame && frame->format == AV_PIX_FMT_NONE)
  253. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  254. if (frame && (frame->width == 0 || frame->height == 0))
  255. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  256. av_assert0(avctx->codec->encode2);
  257. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  258. av_assert0(ret <= 0);
  259. emms_c();
  260. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  261. needs_realloc = 0;
  262. if (user_pkt.data) {
  263. if (user_pkt.size >= avpkt->size) {
  264. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  265. } else {
  266. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  267. avpkt->size = user_pkt.size;
  268. ret = -1;
  269. }
  270. avpkt->buf = user_pkt.buf;
  271. avpkt->data = user_pkt.data;
  272. } else if (!avpkt->buf) {
  273. ret = av_packet_make_refcounted(avpkt);
  274. if (ret < 0)
  275. return ret;
  276. }
  277. }
  278. if (!ret) {
  279. if (!*got_packet_ptr)
  280. avpkt->size = 0;
  281. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  282. avpkt->pts = avpkt->dts = frame->pts;
  283. if (needs_realloc && avpkt->data) {
  284. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  285. if (ret >= 0)
  286. avpkt->data = avpkt->buf->data;
  287. }
  288. if (frame)
  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. int ret;
  362. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  363. return AVERROR_EOF;
  364. ret = avctx->codec->receive_packet(avctx, avpkt);
  365. if (!ret)
  366. // Encoders must always return ref-counted buffers.
  367. // Side-data only packets have no data and can be not ref-counted.
  368. av_assert0(!avpkt->data || avpkt->buf);
  369. return ret;
  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. }