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.

361 lines
11KB

  1. /*
  2. * generic encoding-related code
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  28. int ff_alloc_packet(AVPacket *avpkt, int size)
  29. {
  30. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  31. return AVERROR(EINVAL);
  32. if (avpkt->data) {
  33. AVBufferRef *buf = avpkt->buf;
  34. if (avpkt->size < size)
  35. return AVERROR(EINVAL);
  36. av_init_packet(avpkt);
  37. avpkt->buf = buf;
  38. avpkt->size = size;
  39. return 0;
  40. } else {
  41. return av_new_packet(avpkt, size);
  42. }
  43. }
  44. /**
  45. * Pad last frame with silence.
  46. */
  47. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  48. {
  49. AVFrame *frame = NULL;
  50. int ret;
  51. if (!(frame = av_frame_alloc()))
  52. return AVERROR(ENOMEM);
  53. frame->format = src->format;
  54. frame->channel_layout = src->channel_layout;
  55. frame->nb_samples = s->frame_size;
  56. ret = av_frame_get_buffer(frame, 32);
  57. if (ret < 0)
  58. goto fail;
  59. ret = av_frame_copy_props(frame, src);
  60. if (ret < 0)
  61. goto fail;
  62. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  63. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  64. goto fail;
  65. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  66. frame->nb_samples - src->nb_samples,
  67. s->channels, s->sample_fmt)) < 0)
  68. goto fail;
  69. *dst = frame;
  70. return 0;
  71. fail:
  72. av_frame_free(&frame);
  73. return ret;
  74. }
  75. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  76. AVPacket *avpkt,
  77. const AVFrame *frame,
  78. int *got_packet_ptr)
  79. {
  80. AVFrame tmp;
  81. AVFrame *padded_frame = NULL;
  82. int ret;
  83. int user_packet = !!avpkt->data;
  84. *got_packet_ptr = 0;
  85. if (!avctx->codec->encode2) {
  86. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  87. return AVERROR(ENOSYS);
  88. }
  89. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  90. av_packet_unref(avpkt);
  91. av_init_packet(avpkt);
  92. return 0;
  93. }
  94. /* ensure that extended_data is properly set */
  95. if (frame && !frame->extended_data) {
  96. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  97. avctx->channels > AV_NUM_DATA_POINTERS) {
  98. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  99. "with more than %d channels, but extended_data is not set.\n",
  100. AV_NUM_DATA_POINTERS);
  101. return AVERROR(EINVAL);
  102. }
  103. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  104. tmp = *frame;
  105. tmp.extended_data = tmp.data;
  106. frame = &tmp;
  107. }
  108. /* extract audio service type metadata */
  109. if (frame) {
  110. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  111. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  112. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  113. }
  114. /* check for valid frame size */
  115. if (frame) {
  116. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  117. if (frame->nb_samples > avctx->frame_size)
  118. return AVERROR(EINVAL);
  119. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  120. if (frame->nb_samples < avctx->frame_size &&
  121. !avctx->internal->last_audio_frame) {
  122. ret = pad_last_frame(avctx, &padded_frame, frame);
  123. if (ret < 0)
  124. return ret;
  125. frame = padded_frame;
  126. avctx->internal->last_audio_frame = 1;
  127. }
  128. if (frame->nb_samples != avctx->frame_size) {
  129. ret = AVERROR(EINVAL);
  130. goto end;
  131. }
  132. }
  133. }
  134. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  135. if (!ret) {
  136. if (*got_packet_ptr) {
  137. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  138. if (avpkt->pts == AV_NOPTS_VALUE)
  139. avpkt->pts = frame->pts;
  140. if (!avpkt->duration)
  141. avpkt->duration = ff_samples_to_time_base(avctx,
  142. frame->nb_samples);
  143. }
  144. avpkt->dts = avpkt->pts;
  145. } else {
  146. avpkt->size = 0;
  147. }
  148. if (!user_packet && avpkt->size) {
  149. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  150. if (ret >= 0)
  151. avpkt->data = avpkt->buf->data;
  152. }
  153. avctx->frame_number++;
  154. }
  155. if (ret < 0 || !*got_packet_ptr) {
  156. av_packet_unref(avpkt);
  157. av_init_packet(avpkt);
  158. goto end;
  159. }
  160. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  161. * this needs to be moved to the encoders, but for now we can do it
  162. * here to simplify things */
  163. avpkt->flags |= AV_PKT_FLAG_KEY;
  164. end:
  165. av_frame_free(&padded_frame);
  166. #if FF_API_AUDIOENC_DELAY
  167. avctx->delay = avctx->initial_padding;
  168. #endif
  169. return ret;
  170. }
  171. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  172. AVPacket *avpkt,
  173. const AVFrame *frame,
  174. int *got_packet_ptr)
  175. {
  176. int ret;
  177. int user_packet = !!avpkt->data;
  178. *got_packet_ptr = 0;
  179. if (!avctx->codec->encode2) {
  180. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  181. return AVERROR(ENOSYS);
  182. }
  183. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  184. av_packet_unref(avpkt);
  185. av_init_packet(avpkt);
  186. avpkt->size = 0;
  187. return 0;
  188. }
  189. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  190. return AVERROR(EINVAL);
  191. av_assert0(avctx->codec->encode2);
  192. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  193. if (!ret) {
  194. if (!*got_packet_ptr)
  195. avpkt->size = 0;
  196. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  197. avpkt->pts = avpkt->dts = frame->pts;
  198. if (!user_packet && avpkt->size) {
  199. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  200. if (ret >= 0)
  201. avpkt->data = avpkt->buf->data;
  202. }
  203. avctx->frame_number++;
  204. }
  205. if (ret < 0 || !*got_packet_ptr)
  206. av_packet_unref(avpkt);
  207. emms_c();
  208. return ret;
  209. }
  210. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  211. const AVSubtitle *sub)
  212. {
  213. int ret;
  214. if (sub->start_display_time) {
  215. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  216. return -1;
  217. }
  218. if (sub->num_rects == 0 || !sub->rects)
  219. return -1;
  220. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  221. avctx->frame_number++;
  222. return ret;
  223. }
  224. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  225. {
  226. int ret;
  227. *got_packet = 0;
  228. av_packet_unref(avctx->internal->buffer_pkt);
  229. avctx->internal->buffer_pkt_valid = 0;
  230. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  231. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  232. frame, got_packet);
  233. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  234. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  235. frame, got_packet);
  236. } else {
  237. ret = AVERROR(EINVAL);
  238. }
  239. if (ret >= 0 && *got_packet) {
  240. // Encoders must always return ref-counted buffers.
  241. // Side-data only packets have no data and can be not ref-counted.
  242. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  243. avctx->internal->buffer_pkt_valid = 1;
  244. ret = 0;
  245. } else {
  246. av_packet_unref(avctx->internal->buffer_pkt);
  247. }
  248. return ret;
  249. }
  250. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  251. {
  252. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  253. return AVERROR(EINVAL);
  254. if (avctx->internal->draining)
  255. return AVERROR_EOF;
  256. if (!frame) {
  257. avctx->internal->draining = 1;
  258. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  259. return 0;
  260. }
  261. if (avctx->codec->send_frame)
  262. return avctx->codec->send_frame(avctx, frame);
  263. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  264. // 1. if the AVFrame is not refcounted, the copying will be much more
  265. // expensive than copying the packet data
  266. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  267. // needed
  268. if (avctx->internal->buffer_pkt_valid)
  269. return AVERROR(EAGAIN);
  270. return do_encode(avctx, frame, &(int){0});
  271. }
  272. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  273. {
  274. av_packet_unref(avpkt);
  275. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  276. return AVERROR(EINVAL);
  277. if (avctx->codec->receive_packet) {
  278. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  279. return AVERROR_EOF;
  280. return avctx->codec->receive_packet(avctx, avpkt);
  281. }
  282. // Emulation via old API.
  283. if (!avctx->internal->buffer_pkt_valid) {
  284. int got_packet;
  285. int ret;
  286. if (!avctx->internal->draining)
  287. return AVERROR(EAGAIN);
  288. ret = do_encode(avctx, NULL, &got_packet);
  289. if (ret < 0)
  290. return ret;
  291. if (ret >= 0 && !got_packet)
  292. return AVERROR_EOF;
  293. }
  294. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  295. avctx->internal->buffer_pkt_valid = 0;
  296. return 0;
  297. }