29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/samplefmt.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/avassert.h"
42 #include "libavutil/opt.h"
66 min_size =
FFMAX(17 * min_size / 16 + 32, min_size);
80 static inline int ff_fast_malloc(
void *ptr,
unsigned int *
size,
size_t min_size,
int zero_realloc)
85 min_size =
FFMAX(17 * min_size / 16 + 32, min_size);
136 static int initialized = 0;
138 if (initialized != 0)
152 return codec && codec->
decode;
182 #define INTERNAL_BUFFER_SIZE (32 + 1)
184 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
185 # define STRIDE_ALIGN 16
187 # define STRIDE_ALIGN 8
306 w_align =
FFMAX(w_align, 8);
309 *width =
FFALIGN(*width, w_align);
310 *height =
FFALIGN(*height, h_align);
316 for (i = 0; i < 4; i++)
328 align =
FFMAX(linesize_align[0], linesize_align[3]);
329 linesize_align[1] <<= chroma_shift;
330 linesize_align[2] <<= chroma_shift;
331 align =
FFMAX3(align, linesize_align[1], linesize_align[2]);
332 *width =
FFALIGN(*width, align);
337 int buf_size,
int align)
339 int ch, planar, needed_size, ret = 0;
344 if (buf_size < needed_size)
358 sample_fmt, align)) < 0) {
388 frame->
data[0], buf_size, 0);
397 "internal audio buffer used\n", frame);
439 int h_chroma_shift, v_chroma_shift;
466 for (i = 0; i < 4; i++)
467 unaligned |= picture.
linesize[i] % stride_align[i];
474 for (i = 0; i < 3 && picture.
data[i + 1]; i++)
475 size[i] = picture.
data[i + 1] - picture.
data[i];
476 size[i] = tmpsize - (picture.
data[i] - picture.
data[0]);
478 memset(buf->
base, 0,
sizeof(buf->
base));
479 memset(buf->
data, 0,
sizeof(buf->
data));
481 for (i = 0; i < 4 && size[i]; i++) {
482 const int h_shift = i == 0 ? 0 : h_chroma_shift;
483 const int v_shift = i == 0 ? 0 : v_chroma_shift;
501 if (size[1] && !size[2])
532 int is_chroma = p == 1 || p == 2;
536 for (x = 0; x<bytes; x++)
537 ((uint16_t*)
dst)[x] = c[p];
539 memset(dst, c[p], bytes);
639 av_log(s,
AV_LOG_WARNING,
"Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
681 for (i = 0; i < count; i++) {
682 int r =
func(c, (
char *)arg + i * size);
693 for (i = 0; i < count; i++) {
694 int r =
func(c, arg, i, 0);
716 #if LIBAVCODEC_VERSION_MAJOR >= 55
723 memset(frame, 0,
sizeof(
AVFrame));
755 if (!frame || !*frame)
766 #define MAKE_ACCESSORS(str, name, type, field) \
767 type av_##name##_get_##field(const str *s) { return s->field; } \
768 void av_##name##_set_##field(str *s, type v) { s->field = v; }
787 memset(sub, 0,
sizeof(*sub));
814 #if FF_API_AVCODEC_OPEN
841 if ((!codec && !avctx->
codec)) {
845 if ((codec && avctx->
codec && codec != avctx->
codec)) {
847 "but %s passed to avcodec_open2()\n", avctx->
codec->
name, codec->
name);
851 codec = avctx->
codec;
915 avctx->
codec = codec;
935 "The %s '%s' is experimental but experimental codecs are not enabled, "
936 "add '-strict %d' if you want to use it.\n",
941 codec_string, codec2->
name);
953 av_log(avctx,
AV_LOG_WARNING,
"Warning: not compiled with thread support, using thread emulation\n");
974 av_log(avctx,
AV_LOG_ERROR,
"The maximum value for lowres supported by the decoder is %d\n",
996 av_log(avctx,
AV_LOG_ERROR,
"Specified sample format %s is invalid or not supported\n",
1011 av_log(avctx,
AV_LOG_ERROR,
"Specified pixel format %s is invalid or not supported\n",
1038 av_log(avctx,
AV_LOG_ERROR,
"Specified channel layout '%s' is not supported\n", buf);
1050 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1099 else if (channels != avctx->
channels) {
1103 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1104 "ignoring specified channel layout\n",
1117 "supported with subtitles codecs\n");
1122 "subtitles character encoding will be ignored\n",
1133 "conversion needs a libavcodec built with iconv support "
1134 "for this codec\n");
1166 if (!avpkt->
data || avpkt->
size < size) {
1177 if (avpkt->
size < size) {
1248 int *got_packet_ptr)
1254 int needs_realloc = !user_pkt.
data;
1256 *got_packet_ptr = 0;
1269 "with more than %d channels, but extended_data is not set.\n",
1284 av_log(avctx,
AV_LOG_ERROR,
"more samples than frame size (avcodec_encode_audio2)\n");
1294 frame = padded_frame;
1306 ret = avctx->
codec->
encode2(avctx, avpkt, frame, got_packet_ptr);
1308 if (*got_packet_ptr) {
1323 if (user_pkt.
data) {
1324 if (user_pkt.
size >= avpkt->
size) {
1341 if (needs_realloc && avpkt->
data) {
1344 avpkt->
data = new_data;
1350 if (ret < 0 || !*got_packet_ptr) {
1372 #if FF_API_OLD_ENCODE_AUDIO
1380 int ret, samples_size, got_packet;
1384 pkt.
size = buf_size;
1398 "support this codec\n");
1401 nb_samples = (int64_t)buf_size * 8 /
1404 if (nb_samples >= INT_MAX)
1417 samples_size, 1)) < 0)
1445 return ret ? ret : pkt.
size;
1450 #if FF_API_OLD_ENCODE_VIDEO
1455 int ret, got_packet = 0;
1464 pkt.
size = buf_size;
1481 return ret ? ret : pkt.
size;
1489 int *got_packet_ptr)
1493 int needs_realloc = !user_pkt.
data;
1495 *got_packet_ptr = 0;
1515 ret = avctx->
codec->
encode2(avctx, avpkt, frame, got_packet_ptr);
1520 if (user_pkt.
data) {
1521 if (user_pkt.
size >= avpkt->
size) {
1538 if (!*got_packet_ptr)
1543 if (needs_realloc && avpkt->
data &&
1547 avpkt->
data = new_data;
1553 if (ret < 0 || !*got_packet_ptr)
1585 int64_t reordered_pts, int64_t dts)
1599 pts = reordered_pts;
1616 if (!data || size < 4)
1618 flags = bytestream_get_le32(&data);
1623 avctx->
channels = bytestream_get_le32(&data);
1641 avctx->
width = bytestream_get_le32(&data);
1642 avctx->
height = bytestream_get_le32(&data);
1659 end = side_metadata +
size;
1660 if (size && end[-1])
1662 while (side_metadata < end) {
1663 const uint8_t *key = side_metadata;
1664 const uint8_t *val = side_metadata + strlen(key) + 1;
1673 side_metadata = val + strlen(val) + 1;
1681 int *got_picture_ptr,
1693 *got_picture_ptr = 0;
1707 ret = avctx->
codec->
decode(avctx, picture, got_picture_ptr,
1734 if (*got_picture_ptr){
1751 #if FF_API_OLD_DECODE_AUDIO
1753 int *frame_size_ptr,
1757 int ret, got_frame = 0;
1761 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1763 "avcodec_decode_audio4()\n");
1770 if (ret >= 0 && got_frame) {
1776 if (*frame_size_ptr < data_size) {
1778 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1784 if (planar && avctx->
channels > 1) {
1786 for (ch = 1; ch < avctx->
channels; ch++) {
1791 *frame_size_ptr = data_size;
1793 *frame_size_ptr = 0;
1805 int planar, channels;
1830 ret = avctx->
codec->
decode(avctx, frame, got_frame_ptr, &tmp);
1831 if (ret >= 0 && *got_frame_ptr) {
1850 if(side && side_size>=10) {
1895 if (*got_frame_ptr) {
1907 #define UTF8_MAX_BYTES 4
1912 iconv_t cd = (iconv_t)-1;
1924 if (cd == (iconv_t)-1) {
1926 "with input character encoding \"%s\"\n", avctx->
sub_charenc);
1945 outb = outpkt->
data;
1946 outl = outpkt->
size;
1948 if (iconv(cd, &inb, &inl, &outb, &outl) == (
size_t)-1 ||
1949 iconv(cd,
NULL,
NULL, &outb, &outl) == (
size_t)-1 ||
1950 outl >= outpkt->
size || inl != 0) {
1957 outpkt->
size -= outl;
1958 memset(outpkt->
data + outpkt->
size, 0, outl);
1961 if (cd != (iconv_t)-1)
1965 av_assert0(!
"requesting subtitles recoding without iconv");
2004 avctx->
pkt = &pkt_recoded;
2009 ret = avctx->
codec->
decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2107 default :
return id;
2126 return experimental;
2184 return "unknown_codec";
2189 int i,
len, ret = 0;
2191 #define TAG_PRINT(x) \
2192 (((x) >= '0' && (x) <= '9') || \
2193 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2194 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2196 for (i = 0; i < 4; i++) {
2198 TAG_PRINT(codec_tag & 0xFF) ?
"%c" :
"[%d]", codec_tag & 0xFF);
2200 buf_size = buf_size > len ? buf_size - len : 0;
2210 const char *codec_name;
2211 const char *profile =
NULL;
2216 if (!buf || buf_size <= 0)
2230 snprintf(buf, buf_size,
"%s: %s%s", codec_type ? codec_type :
"unknown",
2232 buf[0] ^=
'a' ^
'A';
2234 snprintf(buf + strlen(buf), buf_size - strlen(buf),
" (%s)", profile);
2238 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2239 " (%s / 0x%04X)", tag_buf, enc->
codec_tag);
2245 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2250 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2254 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2262 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2263 " [SAR %d:%d DAR %d:%d]",
2265 display_aspect_ratio.
num, display_aspect_ratio.
den);
2269 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2275 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2276 ", q=%d-%d", enc->
qmin, enc->
qmax);
2281 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2287 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2295 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2305 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2308 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2313 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2314 ", %d kb/s", bitrate / 1000);
2350 #define LICENSE_PREFIX "libavcodec license: "
2378 for (j = 0; j < 4; j++) {
2474 if (be < 0 || be > 1)
2476 return map[
fmt][be];
2509 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2510 return (frame_bytes * 8LL) / (bps * ch);
2536 return 256 * sr / 245;
2541 return (480 << (sr / 22050)) / ch;
2549 case 20:
return 160;
2550 case 19:
return 144;
2551 case 29:
return 288;
2552 case 37:
return 480;
2556 case 38:
return 160;
2557 case 50:
return 240;
2562 if (frame_bytes > 0) {
2565 return 240 * (frame_bytes / 32);
2567 return 256 * (frame_bytes / 64);
2569 return 160 * (frame_bytes / 20);
2571 return 240 * (frame_bytes / 24);
2576 return frame_bytes * 8 /
bps;
2583 return frame_bytes / (9 * ch) * 16;
2586 return (frame_bytes - 4 * ch) * 2 / ch;
2588 return (frame_bytes - 4) * 2 / ch;
2590 return (frame_bytes - 8) * 2 / ch;
2592 return (frame_bytes / 128) * 224 / ch;
2594 return (frame_bytes - 6 - ch) / ch;
2596 return (frame_bytes - 8) / ch;
2598 return (frame_bytes - 2 * ch) / ch;
2600 return 3 * frame_bytes / ch;
2602 return 6 * frame_bytes / ch;
2604 return 2 * (frame_bytes / (5 * ch));
2607 return 4 * frame_bytes / ch;
2614 return frame_bytes / ch;
2616 return frame_bytes * 2 / ch;
2622 int blocks = frame_bytes / ba;
2625 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2627 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2629 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2631 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2641 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2645 return frame_bytes / ((
FFALIGN(ch, 2) *
bps) / 8);
2647 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2681 for (i = 0; i < size && !(
tab[i][0] == a &&
tab[i][1] ==
b); i++) ;
2688 "version to the newest one from Git. If the problem still "
2689 "occurs, it means that your file has a feature which has not "
2690 "been implemented.\n", feature);
2697 va_list argument_list;
2699 va_start(argument_list, msg);
2704 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2705 "and contact the ffmpeg-devel mailing list.\n");
2707 va_end(argument_list);
2731 if (hwaccel->
id == codec_id
2732 && hwaccel->
pix_fmt == pix_fmt)
2765 av_log(log_ctx,
AV_LOG_ERROR,
"Insufficient thread locking around avcodec_open/close()\n");