219
239
*width=FFALIGN(*width, align);
222
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
242
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
244
AVCodecInternal *avci = avctx->internal;
246
int buf_size, ret, i, needs_extended_data;
248
buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
249
frame->nb_samples, avctx->sample_fmt,
252
return AVERROR(EINVAL);
254
needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
255
avctx->channels > AV_NUM_DATA_POINTERS;
257
/* allocate InternalBuffer if needed */
259
avci->buffer = av_mallocz(sizeof(InternalBuffer));
261
return AVERROR(ENOMEM);
265
/* if there is a previously-used internal buffer, check its size and
266
channel count to see if we can reuse it */
267
if (buf->extended_data) {
268
/* if current buffer is too small, free it */
269
if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
270
av_free(buf->extended_data[0]);
271
if (buf->extended_data != buf->data)
272
av_free(&buf->extended_data);
273
buf->extended_data = NULL;
276
/* if number of channels has changed, reset and/or free extended data
277
pointers but leave data buffer in buf->data[0] for reuse */
278
if (buf->nb_channels != avctx->channels) {
279
if (buf->extended_data != buf->data)
280
av_free(buf->extended_data);
281
buf->extended_data = NULL;
285
/* if there is no previous buffer or the previous buffer cannot be used
286
as-is, allocate a new buffer and/or rearrange the channel pointers */
287
if (!buf->extended_data) {
288
/* if the channel pointers will fit, just set extended_data to data,
289
otherwise allocate the extended_data channel pointers */
290
if (needs_extended_data) {
291
buf->extended_data = av_mallocz(avctx->channels *
292
sizeof(*buf->extended_data));
293
if (!buf->extended_data)
294
return AVERROR(ENOMEM);
296
buf->extended_data = buf->data;
299
/* if there is a previous buffer and it is large enough, reuse it and
300
just fill-in new channel pointers and linesize, otherwise allocate
302
if (buf->extended_data[0]) {
303
ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
304
buf->extended_data[0], avctx->channels,
305
frame->nb_samples, avctx->sample_fmt,
308
ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
309
avctx->channels, frame->nb_samples,
310
avctx->sample_fmt, 32);
315
/* if data was not used for extended_data, we need to copy as many of
316
the extended_data channel pointers as will fit */
317
if (needs_extended_data) {
318
for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
319
buf->data[i] = buf->extended_data[i];
321
buf->audio_data_size = buf_size;
322
buf->nb_channels = avctx->channels;
325
/* copy InternalBuffer info to the AVFrame */
326
frame->type = FF_BUFFER_TYPE_INTERNAL;
327
frame->extended_data = buf->extended_data;
328
frame->linesize[0] = buf->linesize[0];
329
memcpy(frame->data, buf->data, sizeof(frame->data));
331
if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
332
else frame->pkt_pts = AV_NOPTS_VALUE;
333
frame->reordered_opaque = avctx->reordered_opaque;
335
if (avctx->debug & FF_DEBUG_BUFFERS)
336
av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
337
"internal audio buffer used\n", frame);
342
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
225
346
int h= s->height;
226
347
InternalBuffer *buf;
348
AVCodecInternal *avci = s->internal;
229
350
if(pic->data[0]!=NULL) {
230
351
av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
233
if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
234
av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
354
if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
355
av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
238
359
if(av_image_check_size(w, h, 0, s))
241
if(s->internal_buffer==NULL){
242
s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
363
avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
364
sizeof(InternalBuffer));
245
s->internal_buffer= av_fast_realloc(
247
&s->internal_buffer_size,
248
sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
252
buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
253
picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
367
buf = &avci->buffer[avci->buffer_count];
256
369
if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
257
370
if(s->active_thread_type&FF_THREAD_FRAME) {
328
437
buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
439
for (; i < AV_NUM_DATA_POINTERS; i++) {
440
buf->base[i] = buf->data[i] = NULL;
441
buf->linesize[i] = 0;
330
443
if(size[1] && !size[2])
331
444
ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
332
445
buf->width = s->width;
333
446
buf->height = s->height;
334
447
buf->pix_fmt= s->pix_fmt;
335
pic->age= 256*256*256*64;
337
449
pic->type= FF_BUFFER_TYPE_INTERNAL;
451
for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
340
452
pic->base[i]= buf->base[i];
341
453
pic->data[i]= buf->data[i];
342
454
pic->linesize[i]= buf->linesize[i];
344
s->internal_buffer_count++;
456
pic->extended_data = pic->data;
457
avci->buffer_count++;
346
459
if(s->pkt) pic->pkt_pts= s->pkt->pts;
347
460
else pic->pkt_pts= AV_NOPTS_VALUE;
348
461
pic->reordered_opaque= s->reordered_opaque;
350
463
if(s->debug&FF_DEBUG_BUFFERS)
351
av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
464
av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
465
"buffers used\n", pic, avci->buffer_count);
470
int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
472
switch (avctx->codec_type) {
473
case AVMEDIA_TYPE_VIDEO:
474
return video_get_buffer(avctx, frame);
475
case AVMEDIA_TYPE_AUDIO:
476
return audio_get_buffer(avctx, frame);
356
482
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
358
484
InternalBuffer *buf, *last;
485
AVCodecInternal *avci = s->internal;
487
assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
360
489
assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
361
assert(s->internal_buffer_count);
363
if(s->internal_buffer){
364
buf = NULL; /* avoids warning */
365
for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
366
buf= &((InternalBuffer*)s->internal_buffer)[i];
367
if(buf->data[0] == pic->data[0])
370
assert(i < s->internal_buffer_count);
371
s->internal_buffer_count--;
372
last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
374
FFSWAP(InternalBuffer, *buf, *last);
490
assert(avci->buffer_count);
493
buf = NULL; /* avoids warning */
494
for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
495
buf = &avci->buffer[i];
496
if (buf->data[0] == pic->data[0])
499
assert(i < avci->buffer_count);
500
avci->buffer_count--;
501
last = &avci->buffer[avci->buffer_count];
504
FFSWAP(InternalBuffer, *buf, *last);
507
for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
378
508
pic->data[i]=NULL;
379
509
// pic->base[i]=NULL;
381
511
//printf("R%X\n", pic->opaque);
383
513
if(s->debug&FF_DEBUG_BUFFERS)
384
av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
514
av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
515
"buffers used\n", pic, avci->buffer_count);
387
518
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
388
519
AVFrame temp_pic;
522
assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
391
524
/* If no picture return a new buffer */
392
525
if(pic->data[0] == NULL) {
393
526
/* We will copy from buffer, so must be readable */
845
static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
851
if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
854
data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
855
if (!data || size < 4)
857
flags = bytestream_get_le32(&data);
859
if (size < 4) /* Required for any of the changes */
861
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
862
avctx->channels = bytestream_get_le32(&data);
865
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
868
avctx->channel_layout = bytestream_get_le64(&data);
873
if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
874
avctx->sample_rate = bytestream_get_le32(&data);
877
if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
880
avctx->width = bytestream_get_le32(&data);
881
avctx->height = bytestream_get_le32(&data);
882
avcodec_set_dimensions(avctx, avctx->width, avctx->height);
670
887
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
671
888
int *got_picture_ptr,
924
#if FF_API_OLD_DECODE_AUDIO
702
925
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
703
926
int *frame_size_ptr,
930
int ret, got_frame = 0;
932
if (avctx->get_buffer != avcodec_default_get_buffer) {
933
av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
934
"avcodec_decode_audio3()\n");
935
return AVERROR(EINVAL);
938
ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
940
if (ret >= 0 && got_frame) {
942
int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
943
int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
945
avctx->sample_fmt, 1);
946
if (*frame_size_ptr < data_size) {
947
av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
948
"the current frame (%d < %d)\n", *frame_size_ptr, data_size);
949
return AVERROR(EINVAL);
952
memcpy(samples, frame.extended_data[0], plane_size);
954
if (planar && avctx->channels > 1) {
955
uint8_t *out = ((uint8_t *)samples) + plane_size;
956
for (ch = 1; ch < avctx->channels; ch++) {
957
memcpy(out, frame.extended_data[ch], plane_size);
961
*frame_size_ptr = data_size;
969
int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
708
978
avctx->pkt = avpkt;
710
if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
711
//FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
712
if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
713
av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
716
if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
717
*frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
718
av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
722
ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
723
avctx->frame_number++;
980
if (!avpkt->data && avpkt->size) {
981
av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
982
return AVERROR(EINVAL);
985
apply_param_change(avctx, avpkt);
987
if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
988
ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
989
if (ret >= 0 && *got_frame_ptr) {
990
avctx->frame_number++;
991
frame->pkt_dts = avpkt->dts;
992
if (frame->format == AV_SAMPLE_FMT_NONE)
993
frame->format = avctx->sample_fmt;
1039
1308
return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1042
void avcodec_init(void)
1044
static int initialized = 0;
1046
if (initialized != 0)
1050
dsputil_static_init();
1053
1311
void avcodec_flush_buffers(AVCodecContext *avctx)
1055
if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1313
if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1056
1314
ff_thread_flush(avctx);
1057
if(avctx->codec->flush)
1315
else if(avctx->codec->flush)
1058
1316
avctx->codec->flush(avctx);
1061
void avcodec_default_free_buffers(AVCodecContext *s){
1319
static void video_free_buffers(AVCodecContext *s)
1321
AVCodecInternal *avci = s->internal;
1064
if(s->internal_buffer==NULL) return;
1066
if (s->internal_buffer_count)
1067
av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1327
if (avci->buffer_count)
1328
av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1329
avci->buffer_count);
1068
1330
for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1069
InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1331
InternalBuffer *buf = &avci->buffer[i];
1070
1332
for(j=0; j<4; j++){
1071
1333
av_freep(&buf->base[j]);
1072
1334
buf->data[j]= NULL;
1075
av_freep(&s->internal_buffer);
1077
s->internal_buffer_count=0;
1337
av_freep(&avci->buffer);
1339
avci->buffer_count=0;
1342
static void audio_free_buffers(AVCodecContext *avctx)
1344
AVCodecInternal *avci = avctx->internal;
1345
InternalBuffer *buf;
1351
if (buf->extended_data) {
1352
av_free(buf->extended_data[0]);
1353
if (buf->extended_data != buf->data)
1354
av_free(buf->extended_data);
1356
av_freep(&avci->buffer);
1359
void avcodec_default_free_buffers(AVCodecContext *avctx)
1361
switch (avctx->codec_type) {
1362
case AVMEDIA_TYPE_VIDEO:
1363
video_free_buffers(avctx);
1365
case AVMEDIA_TYPE_AUDIO:
1366
audio_free_buffers(avctx);
1080
1373
#if FF_API_OLD_FF_PICT_TYPES