69 #include "libavutil/ffversion.h"
72 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
128 if (min_size <= *size && *p)
130 min_size =
FFMAX(17 * min_size / 16 + 32, min_size);
177 static int initialized = 0;
179 if (initialized != 0)
194 return codec && codec->
decode;
206 last_avcodec = &codec->
next;
219 #if FF_API_SET_DIMENSIONS
274 *data = matrix_encoding;
422 w_align =
FFMAX(w_align, 8);
425 *width =
FFALIGN(*width, w_align);
426 *height =
FFALIGN(*height, h_align);
432 for (i = 0; i < 4; i++)
444 align =
FFMAX(linesize_align[0], linesize_align[3]);
445 linesize_align[1] <<= chroma_shift;
446 linesize_align[2] <<= chroma_shift;
447 align =
FFMAX3(align, linesize_align[1], linesize_align[2]);
448 *width =
FFALIGN(*width, align);
457 *xpos = (pos&1) * 128;
458 *ypos = ((pos>>1)^(pos<4)) * 128;
476 int buf_size,
int align)
478 int ch, planar, needed_size,
ret = 0;
483 if (buf_size < needed_size)
497 sample_fmt, align)) < 0) {
519 int w = frame->
width;
521 int tmpsize, unaligned;
537 for (i = 0; i < 4; i++)
546 for (i = 0; i < 3 && picture.
data[i + 1]; i++)
547 size[i] = picture.
data[i + 1] - picture.
data[i];
548 size[i] = tmpsize - (picture.
data[i] - picture.
data[0]);
550 for (i = 0; i < 4; i++) {
555 CONFIG_MEMORY_POISONING ?
558 if (!pool->
pools[i]) {
573 int planes = planar ? ch : 1;
586 if (!pool->
pools[0]) {
601 for (i = 0; i < 4; i++)
612 int planes = pool->
planes;
664 memset(pic->
data, 0,
sizeof(pic->
data));
667 for (i = 0; i < 4 && pool->
pools[i]; i++) {
701 int is_chroma = p == 1 || p == 2;
704 for (y = 0; y <
height; y++) {
706 for (x = 0; x<bytes; x++)
707 ((uint16_t*)dst)[x] = c[p];
709 memset(dst, c[p], bytes);
722 #if FF_API_GET_BUFFER
742 static const struct {
768 memcpy(frame_sd->
data, packet_sd, size);
835 #if FF_API_GET_BUFFER
872 int override_dimensions = 1;
885 override_dimensions = 0;
902 #if FF_API_GET_BUFFER
935 priv->
avctx = *avctx;
944 #define WRAP_PLANE(ref_out, data, data_size) \
946 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
948 ret = AVERROR(ENOMEM); \
951 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
954 av_frame_unref(frame); \
955 ret = AVERROR(ENOMEM); \
968 if (!desc || planes <= 0) {
973 for (i = 0; i < planes; i++) {
981 planes = planar ? avctx->
channels : 1;
1046 av_log(avctx,
AV_LOG_WARNING,
"Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1053 if (!frame->
data[0])
1085 #if FF_API_GET_BUFFER
1104 for (i = 0; i <
count; i++) {
1105 int r =
func(c, (
char *)arg + i * size);
1116 for (i = 0; i <
count; i++) {
1117 int r =
func(c, arg, i, 0);
1125 unsigned int fourcc)
1128 if (tags->
fourcc == fourcc)
1154 if (hwaccel->
id == codec_id
1155 && hwaccel->
pix_fmt == pix_fmt)
1169 "Could not find an AVHWAccel for the pixel format: %s",
1181 ret = hwa->
init(avctx);
1211 memcpy(choices, fmt, (n + 1) *
sizeof(*choices));
1236 for (n = 0; choices[
n] !=
ret; n++)
1240 choices[
n] = choices[n + 1];
1248 #if FF_API_AVFRAME_LAVC
1251 #if LIBAVCODEC_VERSION_MAJOR >= 55
1258 memset(frame, 0,
sizeof(
AVFrame));
1262 AVFrame *avcodec_alloc_frame(
void)
1267 void avcodec_free_frame(
AVFrame **frame)
1277 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1281 return codec->max_lowres;
1286 memset(sub, 0,
sizeof(*sub));
1293 int bits_per_sample;
1333 if ((!codec && !avctx->
codec)) {
1337 if ((codec && avctx->
codec && codec != avctx->
codec)) {
1339 "but %s passed to avcodec_open2()\n", avctx->
codec->
name, codec->
name);
1343 codec = avctx->
codec;
1437 avctx->
codec = codec;
1457 "The %s '%s' is experimental but experimental codecs are not enabled, "
1458 "add '-strict %d' if you want to use it.\n",
1463 codec_string, codec2->
name);
1475 av_log(avctx,
AV_LOG_WARNING,
"Warning: not compiled with thread support, using thread emulation\n");
1477 if (CONFIG_FRAME_THREAD_ENCODER) {
1496 av_log(avctx,
AV_LOG_ERROR,
"The maximum value for lowres supported by the decoder is %d\n",
1505 "see the codecview filter instead.\n");
1524 av_log(avctx,
AV_LOG_ERROR,
"Specified sample format %s is invalid or not supported\n",
1539 av_log(avctx,
AV_LOG_ERROR,
"Specified pixel format %s is invalid or not supported\n",
1572 av_log(avctx,
AV_LOG_ERROR,
"Specified channel layout '%s' is not supported\n", buf);
1584 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1623 #if FF_API_AUDIOENC_DELAY
1636 else if (channels != avctx->
channels) {
1640 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1641 "ignoring specified channel layout\n",
1654 "supported with subtitles codecs\n");
1659 "subtitles character encoding will be ignored\n",
1670 iconv_t cd = iconv_open(
"UTF-8", avctx->
sub_charenc);
1671 if (cd == (iconv_t)-1) {
1674 "with input character encoding \"%s\"\n", avctx->
sub_charenc);
1680 "conversion needs a libavcodec built with iconv support "
1681 "for this codec\n");
1689 #if FF_API_AVCTX_TIMEBASE
1694 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1708 if (codec->priv_class && codec->priv_data_size)
1711 if (avctx->internal) {
1716 avctx->codec =
NULL;
1722 if (avpkt->
size < 0) {
1727 av_log(avctx,
AV_LOG_ERROR,
"Invalid minimum required packet size %"PRId64
" (max allowed is %d)\n",
1734 if (!avpkt->
data || avpkt->
size < size) {
1738 #if FF_API_DESTRUCT_PACKET
1748 #if FF_API_DESTRUCT_PACKET
1754 if (avpkt->
size < size) {
1760 #if FF_API_DESTRUCT_PACKET
1824 int *got_packet_ptr)
1830 int needs_realloc = !user_pkt.
data;
1832 *got_packet_ptr = 0;
1845 "with more than %d channels, but extended_data is not set.\n",
1852 if (!extended_frame)
1855 memcpy(extended_frame, frame,
sizeof(
AVFrame));
1857 frame = extended_frame;
1871 av_log(avctx,
AV_LOG_ERROR,
"more samples than frame size (avcodec_encode_audio2)\n");
1882 frame = padded_frame;
1894 ret = avctx->
codec->
encode2(avctx, avpkt, frame, got_packet_ptr);
1896 if (*got_packet_ptr) {
1911 if (user_pkt.
data) {
1912 if (user_pkt.
size >= avpkt->
size) {
1919 avpkt->
buf = user_pkt.
buf;
1921 #if FF_API_DESTRUCT_PACKET
1934 if (needs_realloc && avpkt->
data) {
1943 if (ret < 0 || !*got_packet_ptr) {
1958 #if FF_API_AUDIOENC_DELAY
1965 #if FF_API_OLD_ENCODE_AUDIO
1968 const short *samples)
1972 int ret, samples_size, got_packet;
1976 pkt.
size = buf_size;
1991 "support this codec\n");
1995 nb_samples = (int64_t)buf_size * 8 /
1998 if (nb_samples >= INT_MAX) {
2013 samples_size, 1)) < 0) {
2044 return ret ? ret : pkt.
size;
2049 #if FF_API_OLD_ENCODE_VIDEO
2054 int ret, got_packet = 0;
2063 pkt.
size = buf_size;
2080 return ret ? ret : pkt.
size;
2088 int *got_packet_ptr)
2092 int needs_realloc = !user_pkt.
data;
2094 *got_packet_ptr = 0;
2096 if(CONFIG_FRAME_THREAD_ENCODER &&
2115 if (frame && (frame->
width == 0 || frame->
height == 0))
2120 ret = avctx->
codec->
encode2(avctx, avpkt, frame, got_packet_ptr);
2125 if (user_pkt.
data) {
2126 if (user_pkt.
size >= avpkt->
size) {
2133 avpkt->
buf = user_pkt.
buf;
2135 #if FF_API_DESTRUCT_PACKET
2148 if (!*got_packet_ptr)
2153 if (needs_realloc && avpkt->
data) {
2162 if (ret < 0 || !*got_packet_ptr)
2196 int64_t reordered_pts, int64_t dts)
2214 pts = reordered_pts;
2233 "changes, but PARAM_CHANGE side data was sent to it.\n");
2240 flags = bytestream_get_le32(&data);
2246 avctx->
channels = bytestream_get_le32(&data);
2264 avctx->
width = bytestream_get_le32(&data);
2265 avctx->
height = bytestream_get_le32(&data);
2337 int *got_picture_ptr,
2352 *got_picture_ptr = 0;
2372 ret = avctx->
codec->
decode(avctx, picture, got_picture_ptr,
2400 if (*got_picture_ptr) {
2421 #if FF_API_AVCTX_TIMEBASE
2429 #if FF_API_OLD_DECODE_AUDIO
2431 int *frame_size_ptr,
2435 int ret, got_frame = 0;
2441 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2443 "avcodec_decode_audio4()\n");
2450 if (ret >= 0 && got_frame) {
2456 if (*frame_size_ptr < data_size) {
2458 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2465 if (planar && avctx->
channels > 1) {
2467 for (ch = 1; ch < avctx->
channels; ch++) {
2472 *frame_size_ptr = data_size;
2474 *frame_size_ptr = 0;
2508 uint32_t discard_padding = 0;
2525 ret = avctx->
codec->
decode(avctx, frame, got_frame_ptr, &tmp);
2528 if (ret >= 0 && *got_frame_ptr) {
2546 if(side && side_size>=10) {
2550 discard_padding =
AV_RL32(side + 4);
2551 skip_reason =
AV_RL8(side + 8);
2552 discard_reason =
AV_RL8(side + 9);
2584 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
2607 AV_WL32(fside->
data, avctx->internal->skip_samples);
2611 avctx->internal->skip_samples = 0;
2615 avctx->internal->pkt =
NULL;
2622 if (
ret >= 0 && *got_frame_ptr) {
2623 if (!avctx->refcounted_frames) {
2635 #define UTF8_MAX_BYTES 4
2640 iconv_t cd = (iconv_t)-1;
2669 outb = outpkt->
data;
2670 outl = outpkt->
size;
2672 if (iconv(cd, &inb, &inl, &outb, &outl) == (
size_t)-1 ||
2673 iconv(cd,
NULL,
NULL, &outb, &outl) == (
size_t)-1 ||
2674 outl >= outpkt->
size || inl != 0) {
2681 outpkt->
size -= outl;
2682 memset(outpkt->
data + outpkt->
size, 0, outl);
2685 if (cd != (iconv_t)-1)
2697 uint32_t codepoint,
min;
2701 GET_UTF8(codepoint, *(byte++),
return 0;);
2702 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2703 1 << (5 * (byte - str) - 4);
2704 if (codepoint < min || codepoint >= 0x110000 ||
2705 codepoint == 0xFFFE ||
2706 codepoint >= 0xD800 && codepoint <= 0xDFFF )
2759 ret = avctx->
codec->
decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2773 "Invalid UTF-8 in decoded subtitles text; "
2774 "maybe missing -sub_charenc option\n");
2780 if (tmp.
data != pkt_recoded.
data) {
2834 if (CONFIG_FRAME_THREAD_ENCODER &&
2893 default :
return id;
2912 return experimental;
2970 return "unknown_codec";
2977 #define TAG_PRINT(x) \
2978 (((x) >= '0' && (x) <= '9') || \
2979 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2980 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2982 for (i = 0; i < 4; i++) {
2984 TAG_PRINT(codec_tag & 0xFF) ?
"%c" :
"[%d]", codec_tag & 0xFF);
2986 buf_size = buf_size > len ? buf_size - len : 0;
2996 const char *codec_name;
3004 if (!buf || buf_size <= 0)
3018 snprintf(buf, buf_size,
"%s: %s", codec_type ? codec_type :
"unknown",
3020 buf[0] ^=
'a' ^
'A';
3026 snprintf(buf + strlen(buf), buf_size - strlen(buf),
" (%s)", profile);
3031 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3032 " (%s / 0x%04X)", tag_buf, enc->
codec_tag);
3038 char detail[256] =
"(";
3042 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3072 if (strlen(detail) > 1) {
3073 detail[strlen(detail) - 2] = 0;
3079 av_strlcat(buf, new_line ? separator :
", ", buf_size);
3081 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3088 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3096 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3097 " [SAR %d:%d DAR %d:%d]",
3099 display_aspect_ratio.
num, display_aspect_ratio.
den);
3103 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3109 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3110 ", q=%d-%d", enc->
qmin, enc->
qmax);
3117 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3122 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3127 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3134 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3141 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3149 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3152 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3157 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3158 ", %d kb/s", bitrate / 1000);
3160 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3197 return FFMPEG_CONFIGURATION;
3202 #define LICENSE_PREFIX "libavcodec license: "
3290 if (be < 0 || be > 1)
3292 return map[
fmt][be];
3325 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3326 return (frame_bytes * 8LL) / (bps * ch);
3353 return 256 * sr / 245;
3358 return (480 << (sr / 22050)) / ch;
3366 case 20:
return 160;
3367 case 19:
return 144;
3368 case 29:
return 288;
3369 case 37:
return 480;
3373 case 38:
return 160;
3374 case 50:
return 240;
3379 if (frame_bytes > 0) {
3382 return 240 * (frame_bytes / 32);
3384 return 256 * (frame_bytes / 64);
3386 return 160 * (frame_bytes / 20);
3388 return 240 * (frame_bytes / 24);
3393 return frame_bytes * 8 /
bps;
3400 return frame_bytes / (9 * ch) * 16;
3402 return frame_bytes / (16 * ch) * 28;
3405 return (frame_bytes - 4 * ch) * 2 / ch;
3407 return (frame_bytes - 4) * 2 / ch;
3409 return (frame_bytes - 8) * 2 / ch;
3411 return (frame_bytes / 128) * 224 / ch;
3413 return (frame_bytes - 6 - ch) / ch;
3415 return (frame_bytes - 8) / ch;
3417 return (frame_bytes - 2 * ch) / ch;
3419 return 3 * frame_bytes / ch;
3421 return 6 * frame_bytes / ch;
3423 return 2 * (frame_bytes / (5 * ch));
3426 return 4 * frame_bytes / ch;
3433 return frame_bytes / ch;
3435 return frame_bytes * 2 / ch;
3441 int blocks = frame_bytes / ba;
3444 if (bps < 2 || bps > 5)
3446 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3448 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3450 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3452 return blocks * ((ba - 4 * ch) * 2 / ch);
3454 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3464 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3468 return frame_bytes / ((
FFALIGN(ch, 2) *
bps) / 8);
3470 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3515 for (i = 0; i < size && !(
tab[i][0] == a &&
tab[i][1] ==
b); i++) ;
3519 #if FF_API_MISSING_SAMPLE
3524 "version to the newest one from Git. If the problem still "
3525 "occurs, it means that your file has a feature which has not "
3526 "been implemented.\n", feature);
3533 va_list argument_list;
3535 va_start(argument_list, msg);
3540 "of this file to ftp://upload.ffmpeg.org/incoming/ "
3541 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3543 va_end(argument_list);
3557 last_hwaccel = &hwaccel->
next;
3578 void *new_codec_mutex =
NULL;
3579 void *new_avformat_mutex =
NULL;
3605 av_log(log_ctx,
AV_LOG_ERROR,
"Insufficient thread locking around avcodec_open/close()\n");
3607 av_log(log_ctx,
AV_LOG_ERROR,
"No lock manager is set, please see av_lockmgr_register()\n");
3653 ((
unsigned)
av_toupper((x >> 24) & 0xFF) << 24);
3782 uint32_t *av_restrict
state)
3790 for (i = 0; i < 3; i++) {
3791 uint32_t tmp = *state << 8;
3792 *state = tmp + *(p++);
3793 if (tmp == 0x100 || p == end)
3798 if (p[-1] > 1 ) p += 3;
3799 else if (p[-2] ) p += 2;
3800 else if (p[-3]|(p[-1]-1)) p++;
3807 p =
FFMIN(p, end) - 4;