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.

357 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. return ret;
  167. }
  168. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  169. AVPacket *avpkt,
  170. const AVFrame *frame,
  171. int *got_packet_ptr)
  172. {
  173. int ret;
  174. int user_packet = !!avpkt->data;
  175. *got_packet_ptr = 0;
  176. if (!avctx->codec->encode2) {
  177. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  178. return AVERROR(ENOSYS);
  179. }
  180. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  181. av_packet_unref(avpkt);
  182. av_init_packet(avpkt);
  183. avpkt->size = 0;
  184. return 0;
  185. }
  186. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  187. return AVERROR(EINVAL);
  188. av_assert0(avctx->codec->encode2);
  189. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  190. if (!ret) {
  191. if (!*got_packet_ptr)
  192. avpkt->size = 0;
  193. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  194. avpkt->pts = avpkt->dts = frame->pts;
  195. if (!user_packet && avpkt->size) {
  196. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  197. if (ret >= 0)
  198. avpkt->data = avpkt->buf->data;
  199. }
  200. avctx->frame_number++;
  201. }
  202. if (ret < 0 || !*got_packet_ptr)
  203. av_packet_unref(avpkt);
  204. emms_c();
  205. return ret;
  206. }
  207. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  208. const AVSubtitle *sub)
  209. {
  210. int ret;
  211. if (sub->start_display_time) {
  212. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  213. return -1;
  214. }
  215. if (sub->num_rects == 0 || !sub->rects)
  216. return -1;
  217. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  218. avctx->frame_number++;
  219. return ret;
  220. }
  221. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  222. {
  223. int ret;
  224. *got_packet = 0;
  225. av_packet_unref(avctx->internal->buffer_pkt);
  226. avctx->internal->buffer_pkt_valid = 0;
  227. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  228. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  229. frame, got_packet);
  230. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  231. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  232. frame, got_packet);
  233. } else {
  234. ret = AVERROR(EINVAL);
  235. }
  236. if (ret >= 0 && *got_packet) {
  237. // Encoders must always return ref-counted buffers.
  238. // Side-data only packets have no data and can be not ref-counted.
  239. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  240. avctx->internal->buffer_pkt_valid = 1;
  241. ret = 0;
  242. } else {
  243. av_packet_unref(avctx->internal->buffer_pkt);
  244. }
  245. return ret;
  246. }
  247. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  248. {
  249. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  250. return AVERROR(EINVAL);
  251. if (avctx->internal->draining)
  252. return AVERROR_EOF;
  253. if (!frame) {
  254. avctx->internal->draining = 1;
  255. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  256. return 0;
  257. }
  258. if (avctx->codec->send_frame)
  259. return avctx->codec->send_frame(avctx, frame);
  260. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  261. // 1. if the AVFrame is not refcounted, the copying will be much more
  262. // expensive than copying the packet data
  263. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  264. // needed
  265. if (avctx->internal->buffer_pkt_valid)
  266. return AVERROR(EAGAIN);
  267. return do_encode(avctx, frame, &(int){0});
  268. }
  269. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  270. {
  271. av_packet_unref(avpkt);
  272. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  273. return AVERROR(EINVAL);
  274. if (avctx->codec->receive_packet) {
  275. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  276. return AVERROR_EOF;
  277. return avctx->codec->receive_packet(avctx, avpkt);
  278. }
  279. // Emulation via old API.
  280. if (!avctx->internal->buffer_pkt_valid) {
  281. int got_packet;
  282. int ret;
  283. if (!avctx->internal->draining)
  284. return AVERROR(EAGAIN);
  285. ret = do_encode(avctx, NULL, &got_packet);
  286. if (ret < 0)
  287. return ret;
  288. if (ret >= 0 && !got_packet)
  289. return AVERROR_EOF;
  290. }
  291. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  292. avctx->internal->buffer_pkt_valid = 0;
  293. return 0;
  294. }