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.

2413 lines
74KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter=0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if(min_size < *size)
  55. return ptr;
  56. min_size= FFMAX(17*min_size/16 + 32, min_size);
  57. ptr= av_realloc(ptr, min_size);
  58. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  59. min_size= 0;
  60. *size= min_size;
  61. return ptr;
  62. }
  63. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return 0;
  68. min_size= FFMAX(17*min_size/16 + 32, min_size);
  69. av_free(*p);
  70. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  71. if (!*p) min_size = 0;
  72. *size= min_size;
  73. return 1;
  74. }
  75. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. ff_fast_malloc(ptr, size, min_size, 0);
  78. }
  79. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. uint8_t **p = ptr;
  82. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  83. av_freep(p);
  84. *size = 0;
  85. return;
  86. }
  87. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  88. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(AVCodec *c){
  93. if(c) return c->next;
  94. else return first_avcodec;
  95. }
  96. static void avcodec_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized != 0)
  100. return;
  101. initialized = 1;
  102. ff_dsputil_static_init();
  103. }
  104. int av_codec_is_encoder(AVCodec *codec)
  105. {
  106. return codec && (codec->encode || codec->encode2);
  107. }
  108. int av_codec_is_decoder(AVCodec *codec)
  109. {
  110. return codec && codec->decode;
  111. }
  112. void avcodec_register(AVCodec *codec)
  113. {
  114. AVCodec **p;
  115. avcodec_init();
  116. p = &first_avcodec;
  117. while (*p != NULL) p = &(*p)->next;
  118. *p = codec;
  119. codec->next = NULL;
  120. if (codec->init_static_data)
  121. codec->init_static_data(codec);
  122. }
  123. unsigned avcodec_get_edge_width(void)
  124. {
  125. return EDGE_WIDTH;
  126. }
  127. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  128. s->coded_width = width;
  129. s->coded_height= height;
  130. s->width = -((-width )>>s->lowres);
  131. s->height= -((-height)>>s->lowres);
  132. }
  133. #define INTERNAL_BUFFER_SIZE (32+1)
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align= 1;
  139. int h_align= 1;
  140. switch(s->pix_fmt){
  141. case PIX_FMT_YUV420P:
  142. case PIX_FMT_YUYV422:
  143. case PIX_FMT_UYVY422:
  144. case PIX_FMT_YUV422P:
  145. case PIX_FMT_YUV440P:
  146. case PIX_FMT_YUV444P:
  147. case PIX_FMT_GBRP:
  148. case PIX_FMT_GRAY8:
  149. case PIX_FMT_GRAY16BE:
  150. case PIX_FMT_GRAY16LE:
  151. case PIX_FMT_YUVJ420P:
  152. case PIX_FMT_YUVJ422P:
  153. case PIX_FMT_YUVJ440P:
  154. case PIX_FMT_YUVJ444P:
  155. case PIX_FMT_YUVA420P:
  156. case PIX_FMT_YUVA422P:
  157. case PIX_FMT_YUVA444P:
  158. case PIX_FMT_YUV420P9LE:
  159. case PIX_FMT_YUV420P9BE:
  160. case PIX_FMT_YUV420P10LE:
  161. case PIX_FMT_YUV420P10BE:
  162. case PIX_FMT_YUV422P9LE:
  163. case PIX_FMT_YUV422P9BE:
  164. case PIX_FMT_YUV422P10LE:
  165. case PIX_FMT_YUV422P10BE:
  166. case PIX_FMT_YUV444P9LE:
  167. case PIX_FMT_YUV444P9BE:
  168. case PIX_FMT_YUV444P10LE:
  169. case PIX_FMT_YUV444P10BE:
  170. case PIX_FMT_GBRP9LE:
  171. case PIX_FMT_GBRP9BE:
  172. case PIX_FMT_GBRP10LE:
  173. case PIX_FMT_GBRP10BE:
  174. w_align = 16; //FIXME assume 16 pixel per macroblock
  175. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  176. break;
  177. case PIX_FMT_YUV411P:
  178. case PIX_FMT_UYYVYY411:
  179. w_align=32;
  180. h_align=8;
  181. break;
  182. case PIX_FMT_YUV410P:
  183. if(s->codec_id == CODEC_ID_SVQ1){
  184. w_align=64;
  185. h_align=64;
  186. }
  187. case PIX_FMT_RGB555:
  188. if(s->codec_id == CODEC_ID_RPZA){
  189. w_align=4;
  190. h_align=4;
  191. }
  192. case PIX_FMT_PAL8:
  193. case PIX_FMT_BGR8:
  194. case PIX_FMT_RGB8:
  195. if(s->codec_id == CODEC_ID_SMC){
  196. w_align=4;
  197. h_align=4;
  198. }
  199. break;
  200. case PIX_FMT_BGR24:
  201. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  202. w_align=4;
  203. h_align=4;
  204. }
  205. break;
  206. default:
  207. w_align= 1;
  208. h_align= 1;
  209. break;
  210. }
  211. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  212. w_align= FFMAX(w_align, 8);
  213. }
  214. *width = FFALIGN(*width , w_align);
  215. *height= FFALIGN(*height, h_align);
  216. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  217. *height+=2; // some of the optimized chroma MC reads one line too much
  218. // which is also done in mpeg decoders with lowres > 0
  219. for (i = 0; i < 4; i++)
  220. linesize_align[i] = STRIDE_ALIGN;
  221. }
  222. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  223. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  224. int linesize_align[AV_NUM_DATA_POINTERS];
  225. int align;
  226. avcodec_align_dimensions2(s, width, height, linesize_align);
  227. align = FFMAX(linesize_align[0], linesize_align[3]);
  228. linesize_align[1] <<= chroma_shift;
  229. linesize_align[2] <<= chroma_shift;
  230. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  231. *width=FFALIGN(*width, align);
  232. }
  233. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  234. {
  235. if (s->pkt) {
  236. pic->pkt_pts = s->pkt->pts;
  237. pic->pkt_pos = s->pkt->pos;
  238. pic->pkt_duration = s->pkt->duration;
  239. } else {
  240. pic->pkt_pts = AV_NOPTS_VALUE;
  241. pic->pkt_pos = -1;
  242. pic->pkt_duration = 0;
  243. }
  244. pic->reordered_opaque= s->reordered_opaque;
  245. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  246. pic->width = s->width;
  247. pic->height = s->height;
  248. pic->format = s->pix_fmt;
  249. }
  250. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  251. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  252. int buf_size, int align)
  253. {
  254. int ch, planar, needed_size, ret = 0;
  255. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  256. frame->nb_samples, sample_fmt,
  257. align);
  258. if (buf_size < needed_size)
  259. return AVERROR(EINVAL);
  260. planar = av_sample_fmt_is_planar(sample_fmt);
  261. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  262. if (!(frame->extended_data = av_mallocz(nb_channels *
  263. sizeof(*frame->extended_data))))
  264. return AVERROR(ENOMEM);
  265. } else {
  266. frame->extended_data = frame->data;
  267. }
  268. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  269. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  270. sample_fmt, align)) < 0) {
  271. if (frame->extended_data != frame->data)
  272. av_freep(&frame->extended_data);
  273. return ret;
  274. }
  275. if (frame->extended_data != frame->data) {
  276. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  277. frame->data[ch] = frame->extended_data[ch];
  278. }
  279. return ret;
  280. }
  281. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  282. {
  283. AVCodecInternal *avci = avctx->internal;
  284. InternalBuffer *buf;
  285. int buf_size, ret;
  286. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  287. frame->nb_samples, avctx->sample_fmt,
  288. 0);
  289. if (buf_size < 0)
  290. return AVERROR(EINVAL);
  291. /* allocate InternalBuffer if needed */
  292. if (!avci->buffer) {
  293. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  294. if (!avci->buffer)
  295. return AVERROR(ENOMEM);
  296. }
  297. buf = avci->buffer;
  298. /* if there is a previously-used internal buffer, check its size and
  299. channel count to see if we can reuse it */
  300. if (buf->extended_data) {
  301. /* if current buffer is too small, free it */
  302. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  303. av_free(buf->extended_data[0]);
  304. if (buf->extended_data != buf->data)
  305. av_freep(&buf->extended_data);
  306. buf->extended_data = NULL;
  307. buf->data[0] = NULL;
  308. }
  309. /* if number of channels has changed, reset and/or free extended data
  310. pointers but leave data buffer in buf->data[0] for reuse */
  311. if (buf->nb_channels != avctx->channels) {
  312. if (buf->extended_data != buf->data)
  313. av_free(buf->extended_data);
  314. buf->extended_data = NULL;
  315. }
  316. }
  317. /* if there is no previous buffer or the previous buffer cannot be used
  318. as-is, allocate a new buffer and/or rearrange the channel pointers */
  319. if (!buf->extended_data) {
  320. if (!buf->data[0]) {
  321. if (!(buf->data[0] = av_mallocz(buf_size)))
  322. return AVERROR(ENOMEM);
  323. buf->audio_data_size = buf_size;
  324. }
  325. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  326. avctx->sample_fmt, buf->data[0],
  327. buf->audio_data_size, 0)))
  328. return ret;
  329. if (frame->extended_data == frame->data)
  330. buf->extended_data = buf->data;
  331. else
  332. buf->extended_data = frame->extended_data;
  333. memcpy(buf->data, frame->data, sizeof(frame->data));
  334. buf->linesize[0] = frame->linesize[0];
  335. buf->nb_channels = avctx->channels;
  336. } else {
  337. /* copy InternalBuffer info to the AVFrame */
  338. frame->extended_data = buf->extended_data;
  339. frame->linesize[0] = buf->linesize[0];
  340. memcpy(frame->data, buf->data, sizeof(frame->data));
  341. }
  342. frame->type = FF_BUFFER_TYPE_INTERNAL;
  343. if (avctx->pkt) {
  344. frame->pkt_pts = avctx->pkt->pts;
  345. frame->pkt_pos = avctx->pkt->pos;
  346. frame->pkt_duration = avctx->pkt->duration;
  347. } else {
  348. frame->pkt_pts = AV_NOPTS_VALUE;
  349. frame->pkt_pos = -1;
  350. frame->pkt_duration = 0;
  351. }
  352. frame->reordered_opaque = avctx->reordered_opaque;
  353. frame->sample_rate = avctx->sample_rate;
  354. frame->format = avctx->sample_fmt;
  355. frame->channel_layout = avctx->channel_layout;
  356. if (avctx->debug & FF_DEBUG_BUFFERS)
  357. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  358. "internal audio buffer used\n", frame);
  359. return 0;
  360. }
  361. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  362. {
  363. int i;
  364. int w= s->width;
  365. int h= s->height;
  366. InternalBuffer *buf;
  367. AVCodecInternal *avci = s->internal;
  368. if(pic->data[0]!=NULL) {
  369. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  370. return -1;
  371. }
  372. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  373. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  374. return -1;
  375. }
  376. if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
  377. return -1;
  378. if (!avci->buffer) {
  379. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  380. sizeof(InternalBuffer));
  381. }
  382. buf = &avci->buffer[avci->buffer_count];
  383. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  384. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  385. av_freep(&buf->base[i]);
  386. buf->data[i]= NULL;
  387. }
  388. }
  389. if (!buf->base[0]) {
  390. int h_chroma_shift, v_chroma_shift;
  391. int size[4] = {0};
  392. int tmpsize;
  393. int unaligned;
  394. AVPicture picture;
  395. int stride_align[AV_NUM_DATA_POINTERS];
  396. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  397. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  398. avcodec_align_dimensions2(s, &w, &h, stride_align);
  399. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  400. w+= EDGE_WIDTH*2;
  401. h+= EDGE_WIDTH*2;
  402. }
  403. do {
  404. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  405. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  406. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  407. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  408. w += w & ~(w-1);
  409. unaligned = 0;
  410. for (i=0; i<4; i++){
  411. unaligned |= picture.linesize[i] % stride_align[i];
  412. }
  413. } while (unaligned);
  414. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  415. if (tmpsize < 0)
  416. return -1;
  417. for (i=0; i<3 && picture.data[i+1]; i++)
  418. size[i] = picture.data[i+1] - picture.data[i];
  419. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  420. memset(buf->base, 0, sizeof(buf->base));
  421. memset(buf->data, 0, sizeof(buf->data));
  422. for(i=0; i<4 && size[i]; i++){
  423. const int h_shift= i==0 ? 0 : h_chroma_shift;
  424. const int v_shift= i==0 ? 0 : v_chroma_shift;
  425. buf->linesize[i]= picture.linesize[i];
  426. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  427. if(buf->base[i]==NULL) return -1;
  428. memset(buf->base[i], 128, size[i]);
  429. // no edge if EDGE EMU or not planar YUV
  430. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  431. buf->data[i] = buf->base[i];
  432. else
  433. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  434. }
  435. for (; i < AV_NUM_DATA_POINTERS; i++) {
  436. buf->base[i] = buf->data[i] = NULL;
  437. buf->linesize[i] = 0;
  438. }
  439. if(size[1] && !size[2])
  440. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  441. buf->width = s->width;
  442. buf->height = s->height;
  443. buf->pix_fmt= s->pix_fmt;
  444. }
  445. pic->type= FF_BUFFER_TYPE_INTERNAL;
  446. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  447. pic->base[i]= buf->base[i];
  448. pic->data[i]= buf->data[i];
  449. pic->linesize[i]= buf->linesize[i];
  450. }
  451. pic->extended_data = pic->data;
  452. avci->buffer_count++;
  453. pic->width = buf->width;
  454. pic->height = buf->height;
  455. pic->format = buf->pix_fmt;
  456. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  457. if (s->pkt) {
  458. pic->pkt_pts = s->pkt->pts;
  459. pic->pkt_pos = s->pkt->pos;
  460. pic->pkt_duration = s->pkt->duration;
  461. } else {
  462. pic->pkt_pts = AV_NOPTS_VALUE;
  463. pic->pkt_pos = -1;
  464. pic->pkt_duration = 0;
  465. }
  466. pic->reordered_opaque= s->reordered_opaque;
  467. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  468. pic->width = s->width;
  469. pic->height = s->height;
  470. pic->format = s->pix_fmt;
  471. if(s->debug&FF_DEBUG_BUFFERS)
  472. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  473. "buffers used\n", pic, avci->buffer_count);
  474. return 0;
  475. }
  476. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  477. {
  478. switch (avctx->codec_type) {
  479. case AVMEDIA_TYPE_VIDEO:
  480. return video_get_buffer(avctx, frame);
  481. case AVMEDIA_TYPE_AUDIO:
  482. return audio_get_buffer(avctx, frame);
  483. default:
  484. return -1;
  485. }
  486. }
  487. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  488. int i;
  489. InternalBuffer *buf, *last;
  490. AVCodecInternal *avci = s->internal;
  491. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  492. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  493. assert(avci->buffer_count);
  494. if (avci->buffer) {
  495. buf = NULL; /* avoids warning */
  496. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  497. buf = &avci->buffer[i];
  498. if (buf->data[0] == pic->data[0])
  499. break;
  500. }
  501. av_assert0(i < avci->buffer_count);
  502. avci->buffer_count--;
  503. last = &avci->buffer[avci->buffer_count];
  504. if (buf != last)
  505. FFSWAP(InternalBuffer, *buf, *last);
  506. }
  507. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  508. pic->data[i]=NULL;
  509. // pic->base[i]=NULL;
  510. }
  511. //printf("R%X\n", pic->opaque);
  512. if(s->debug&FF_DEBUG_BUFFERS)
  513. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  514. "buffers used\n", pic, avci->buffer_count);
  515. }
  516. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  517. AVFrame temp_pic;
  518. int i;
  519. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  520. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  521. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  522. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  523. s->release_buffer(s, pic);
  524. }
  525. ff_init_buffer_info(s, pic);
  526. /* If no picture return a new buffer */
  527. if(pic->data[0] == NULL) {
  528. /* We will copy from buffer, so must be readable */
  529. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  530. return s->get_buffer(s, pic);
  531. }
  532. assert(s->pix_fmt == pic->format);
  533. /* If internal buffer type return the same buffer */
  534. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  535. return 0;
  536. }
  537. /*
  538. * Not internal type and reget_buffer not overridden, emulate cr buffer
  539. */
  540. temp_pic = *pic;
  541. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  542. pic->data[i] = pic->base[i] = NULL;
  543. pic->opaque = NULL;
  544. /* Allocate new frame */
  545. if (s->get_buffer(s, pic))
  546. return -1;
  547. /* Copy image data from old buffer to new buffer */
  548. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  549. s->height);
  550. s->release_buffer(s, &temp_pic); // Release old frame
  551. return 0;
  552. }
  553. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  554. int i;
  555. for(i=0; i<count; i++){
  556. int r= func(c, (char*)arg + i*size);
  557. if(ret) ret[i]= r;
  558. }
  559. return 0;
  560. }
  561. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  562. int i;
  563. for(i=0; i<count; i++){
  564. int r= func(c, arg, i, 0);
  565. if(ret) ret[i]= r;
  566. }
  567. return 0;
  568. }
  569. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  570. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  571. ++fmt;
  572. return fmt[0];
  573. }
  574. void avcodec_get_frame_defaults(AVFrame *pic){
  575. memset(pic, 0, sizeof(AVFrame));
  576. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  577. pic->pkt_duration = 0;
  578. pic->pkt_pos = -1;
  579. pic->key_frame= 1;
  580. pic->sample_aspect_ratio = (AVRational){0, 1};
  581. pic->format = -1; /* unknown */
  582. }
  583. AVFrame *avcodec_alloc_frame(void){
  584. AVFrame *pic= av_malloc(sizeof(AVFrame));
  585. if(pic==NULL) return NULL;
  586. avcodec_get_frame_defaults(pic);
  587. return pic;
  588. }
  589. #define MAKE_ACCESSORS(str, name, type, field) \
  590. type av_##name##_get_##field(const str *s) { return s->field; } \
  591. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  592. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  593. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  594. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  595. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  596. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  597. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  598. {
  599. memset(sub, 0, sizeof(*sub));
  600. sub->pts = AV_NOPTS_VALUE;
  601. }
  602. static int get_bit_rate(AVCodecContext *ctx)
  603. {
  604. int bit_rate;
  605. int bits_per_sample;
  606. switch(ctx->codec_type) {
  607. case AVMEDIA_TYPE_VIDEO:
  608. case AVMEDIA_TYPE_DATA:
  609. case AVMEDIA_TYPE_SUBTITLE:
  610. case AVMEDIA_TYPE_ATTACHMENT:
  611. bit_rate = ctx->bit_rate;
  612. break;
  613. case AVMEDIA_TYPE_AUDIO:
  614. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  615. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  616. break;
  617. default:
  618. bit_rate = 0;
  619. break;
  620. }
  621. return bit_rate;
  622. }
  623. #if FF_API_AVCODEC_OPEN
  624. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  625. {
  626. return avcodec_open2(avctx, codec, NULL);
  627. }
  628. #endif
  629. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  630. {
  631. int ret = 0;
  632. AVDictionary *tmp = NULL;
  633. if (avcodec_is_open(avctx))
  634. return 0;
  635. if ((!codec && !avctx->codec)) {
  636. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  637. return AVERROR(EINVAL);
  638. }
  639. if ((codec && avctx->codec && codec != avctx->codec)) {
  640. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  641. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  642. return AVERROR(EINVAL);
  643. }
  644. if (!codec)
  645. codec = avctx->codec;
  646. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  647. return AVERROR(EINVAL);
  648. if (options)
  649. av_dict_copy(&tmp, *options, 0);
  650. /* If there is a user-supplied mutex locking routine, call it. */
  651. if (ff_lockmgr_cb) {
  652. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  653. return -1;
  654. }
  655. entangled_thread_counter++;
  656. if(entangled_thread_counter != 1){
  657. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  658. ret = -1;
  659. goto end;
  660. }
  661. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  662. if (!avctx->internal) {
  663. ret = AVERROR(ENOMEM);
  664. goto end;
  665. }
  666. if (codec->priv_data_size > 0) {
  667. if(!avctx->priv_data){
  668. avctx->priv_data = av_mallocz(codec->priv_data_size);
  669. if (!avctx->priv_data) {
  670. ret = AVERROR(ENOMEM);
  671. goto end;
  672. }
  673. if (codec->priv_class) {
  674. *(const AVClass**)avctx->priv_data= codec->priv_class;
  675. av_opt_set_defaults(avctx->priv_data);
  676. }
  677. }
  678. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  679. goto free_and_end;
  680. } else {
  681. avctx->priv_data = NULL;
  682. }
  683. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  684. goto free_and_end;
  685. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  686. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  687. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  688. ret = -1;
  689. goto free_and_end;
  690. }
  691. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  692. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  693. if(avctx->coded_width && avctx->coded_height)
  694. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  695. else if(avctx->width && avctx->height)
  696. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  697. }
  698. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  699. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  700. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  701. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  702. avcodec_set_dimensions(avctx, 0, 0);
  703. }
  704. /* if the decoder init function was already called previously,
  705. free the already allocated subtitle_header before overwriting it */
  706. if (av_codec_is_decoder(codec))
  707. av_freep(&avctx->subtitle_header);
  708. #define SANE_NB_CHANNELS 128U
  709. if (avctx->channels > SANE_NB_CHANNELS) {
  710. ret = AVERROR(EINVAL);
  711. goto free_and_end;
  712. }
  713. avctx->codec = codec;
  714. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  715. avctx->codec_id == CODEC_ID_NONE) {
  716. avctx->codec_type = codec->type;
  717. avctx->codec_id = codec->id;
  718. }
  719. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  720. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  721. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  722. ret = AVERROR(EINVAL);
  723. goto free_and_end;
  724. }
  725. avctx->frame_number = 0;
  726. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  727. (!avctx->time_base.num || !avctx->time_base.den)) {
  728. avctx->time_base.num = 1;
  729. avctx->time_base.den = avctx->sample_rate;
  730. }
  731. if (!HAVE_THREADS)
  732. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  733. if (HAVE_THREADS && !avctx->thread_opaque) {
  734. ret = ff_thread_init(avctx);
  735. if (ret < 0) {
  736. goto free_and_end;
  737. }
  738. }
  739. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  740. avctx->thread_count = 1;
  741. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  742. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  743. avctx->codec->max_lowres);
  744. ret = AVERROR(EINVAL);
  745. goto free_and_end;
  746. }
  747. if (av_codec_is_encoder(avctx->codec)) {
  748. int i;
  749. if (avctx->codec->sample_fmts) {
  750. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  751. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  752. break;
  753. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  754. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  755. ret = AVERROR(EINVAL);
  756. goto free_and_end;
  757. }
  758. }
  759. if (avctx->codec->pix_fmts) {
  760. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  761. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  762. break;
  763. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
  764. && !((avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG)
  765. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  766. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  767. ret = AVERROR(EINVAL);
  768. goto free_and_end;
  769. }
  770. }
  771. if (avctx->codec->supported_samplerates) {
  772. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  773. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  774. break;
  775. if (avctx->codec->supported_samplerates[i] == 0) {
  776. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  777. ret = AVERROR(EINVAL);
  778. goto free_and_end;
  779. }
  780. }
  781. if (avctx->codec->channel_layouts) {
  782. if (!avctx->channel_layout) {
  783. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  784. } else {
  785. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  786. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  787. break;
  788. if (avctx->codec->channel_layouts[i] == 0) {
  789. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  790. ret = AVERROR(EINVAL);
  791. goto free_and_end;
  792. }
  793. }
  794. }
  795. if (avctx->channel_layout && avctx->channels) {
  796. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  797. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  798. ret = AVERROR(EINVAL);
  799. goto free_and_end;
  800. }
  801. } else if (avctx->channel_layout) {
  802. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  803. }
  804. }
  805. avctx->pts_correction_num_faulty_pts =
  806. avctx->pts_correction_num_faulty_dts = 0;
  807. avctx->pts_correction_last_pts =
  808. avctx->pts_correction_last_dts = INT64_MIN;
  809. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  810. ret = avctx->codec->init(avctx);
  811. if (ret < 0) {
  812. goto free_and_end;
  813. }
  814. }
  815. ret=0;
  816. if (av_codec_is_decoder(avctx->codec)) {
  817. if (!avctx->bit_rate)
  818. avctx->bit_rate = get_bit_rate(avctx);
  819. /* validate channel layout from the decoder */
  820. if (avctx->channel_layout &&
  821. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  822. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  823. avctx->channel_layout = 0;
  824. }
  825. }
  826. end:
  827. entangled_thread_counter--;
  828. /* Release any user-supplied mutex. */
  829. if (ff_lockmgr_cb) {
  830. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  831. }
  832. if (options) {
  833. av_dict_free(options);
  834. *options = tmp;
  835. }
  836. return ret;
  837. free_and_end:
  838. av_dict_free(&tmp);
  839. av_freep(&avctx->priv_data);
  840. av_freep(&avctx->internal);
  841. avctx->codec= NULL;
  842. goto end;
  843. }
  844. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  845. {
  846. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  847. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  848. return AVERROR(EINVAL);
  849. }
  850. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  851. if (!avpkt->data || avpkt->size < size) {
  852. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  853. avpkt->data = avctx->internal->byte_buffer;
  854. avpkt->size = avctx->internal->byte_buffer_size;
  855. avpkt->destruct = NULL;
  856. }
  857. if (avpkt->data) {
  858. void *destruct = avpkt->destruct;
  859. if (avpkt->size < size) {
  860. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  861. return AVERROR(EINVAL);
  862. }
  863. av_init_packet(avpkt);
  864. avpkt->destruct = destruct;
  865. avpkt->size = size;
  866. return 0;
  867. } else {
  868. int ret = av_new_packet(avpkt, size);
  869. if (ret < 0)
  870. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  871. return ret;
  872. }
  873. }
  874. int ff_alloc_packet(AVPacket *avpkt, int size)
  875. {
  876. return ff_alloc_packet2(NULL, avpkt, size);
  877. }
  878. /**
  879. * Pad last frame with silence.
  880. */
  881. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  882. {
  883. AVFrame *frame = NULL;
  884. uint8_t *buf = NULL;
  885. int ret;
  886. if (!(frame = avcodec_alloc_frame()))
  887. return AVERROR(ENOMEM);
  888. *frame = *src;
  889. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  890. s->frame_size, s->sample_fmt, 0)) < 0)
  891. goto fail;
  892. if (!(buf = av_malloc(ret))) {
  893. ret = AVERROR(ENOMEM);
  894. goto fail;
  895. }
  896. frame->nb_samples = s->frame_size;
  897. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  898. buf, ret, 0)) < 0)
  899. goto fail;
  900. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  901. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  902. goto fail;
  903. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  904. frame->nb_samples - src->nb_samples,
  905. s->channels, s->sample_fmt)) < 0)
  906. goto fail;
  907. *dst = frame;
  908. return 0;
  909. fail:
  910. if (frame->extended_data != frame->data)
  911. av_freep(&frame->extended_data);
  912. av_freep(&buf);
  913. av_freep(&frame);
  914. return ret;
  915. }
  916. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  917. AVPacket *avpkt,
  918. const AVFrame *frame,
  919. int *got_packet_ptr)
  920. {
  921. AVFrame tmp;
  922. AVFrame *padded_frame = NULL;
  923. int ret;
  924. AVPacket user_pkt = *avpkt;
  925. int needs_realloc = !user_pkt.data;
  926. *got_packet_ptr = 0;
  927. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  928. av_free_packet(avpkt);
  929. av_init_packet(avpkt);
  930. return 0;
  931. }
  932. /* ensure that extended_data is properly set */
  933. if (frame && !frame->extended_data) {
  934. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  935. avctx->channels > AV_NUM_DATA_POINTERS) {
  936. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  937. "with more than %d channels, but extended_data is not set.\n",
  938. AV_NUM_DATA_POINTERS);
  939. return AVERROR(EINVAL);
  940. }
  941. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  942. tmp = *frame;
  943. tmp.extended_data = tmp.data;
  944. frame = &tmp;
  945. }
  946. /* check for valid frame size */
  947. if (frame) {
  948. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  949. if (frame->nb_samples > avctx->frame_size)
  950. return AVERROR(EINVAL);
  951. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  952. if (frame->nb_samples < avctx->frame_size &&
  953. !avctx->internal->last_audio_frame) {
  954. ret = pad_last_frame(avctx, &padded_frame, frame);
  955. if (ret < 0)
  956. return ret;
  957. frame = padded_frame;
  958. avctx->internal->last_audio_frame = 1;
  959. }
  960. if (frame->nb_samples != avctx->frame_size)
  961. return AVERROR(EINVAL);
  962. }
  963. }
  964. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  965. if (!ret) {
  966. if (*got_packet_ptr) {
  967. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  968. if (avpkt->pts == AV_NOPTS_VALUE)
  969. avpkt->pts = frame->pts;
  970. if (!avpkt->duration)
  971. avpkt->duration = ff_samples_to_time_base(avctx,
  972. frame->nb_samples);
  973. }
  974. avpkt->dts = avpkt->pts;
  975. } else {
  976. avpkt->size = 0;
  977. }
  978. }
  979. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  980. needs_realloc = 0;
  981. if (user_pkt.data) {
  982. if (user_pkt.size >= avpkt->size) {
  983. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  984. } else {
  985. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  986. avpkt->size = user_pkt.size;
  987. ret = -1;
  988. }
  989. avpkt->data = user_pkt.data;
  990. avpkt->destruct = user_pkt.destruct;
  991. } else {
  992. if (av_dup_packet(avpkt) < 0) {
  993. ret = AVERROR(ENOMEM);
  994. }
  995. }
  996. }
  997. if (!ret) {
  998. if (needs_realloc && avpkt->data) {
  999. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1000. if (new_data)
  1001. avpkt->data = new_data;
  1002. }
  1003. avctx->frame_number++;
  1004. }
  1005. if (ret < 0 || !*got_packet_ptr) {
  1006. av_free_packet(avpkt);
  1007. av_init_packet(avpkt);
  1008. return ret;
  1009. }
  1010. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1011. this needs to be moved to the encoders, but for now we can do it
  1012. here to simplify things */
  1013. avpkt->flags |= AV_PKT_FLAG_KEY;
  1014. if (padded_frame) {
  1015. av_freep(&padded_frame->data[0]);
  1016. if (padded_frame->extended_data != padded_frame->data)
  1017. av_freep(&padded_frame->extended_data);
  1018. av_freep(&padded_frame);
  1019. }
  1020. return ret;
  1021. }
  1022. #if FF_API_OLD_DECODE_AUDIO
  1023. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1024. uint8_t *buf, int buf_size,
  1025. const short *samples)
  1026. {
  1027. AVPacket pkt;
  1028. AVFrame frame0;
  1029. AVFrame *frame;
  1030. int ret, samples_size, got_packet;
  1031. av_init_packet(&pkt);
  1032. pkt.data = buf;
  1033. pkt.size = buf_size;
  1034. if (samples) {
  1035. frame = &frame0;
  1036. avcodec_get_frame_defaults(frame);
  1037. if (avctx->frame_size) {
  1038. frame->nb_samples = avctx->frame_size;
  1039. } else {
  1040. /* if frame_size is not set, the number of samples must be
  1041. calculated from the buffer size */
  1042. int64_t nb_samples;
  1043. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1044. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1045. "support this codec\n");
  1046. return AVERROR(EINVAL);
  1047. }
  1048. nb_samples = (int64_t)buf_size * 8 /
  1049. (av_get_bits_per_sample(avctx->codec_id) *
  1050. avctx->channels);
  1051. if (nb_samples >= INT_MAX)
  1052. return AVERROR(EINVAL);
  1053. frame->nb_samples = nb_samples;
  1054. }
  1055. /* it is assumed that the samples buffer is large enough based on the
  1056. relevant parameters */
  1057. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1058. frame->nb_samples,
  1059. avctx->sample_fmt, 1);
  1060. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1061. avctx->sample_fmt,
  1062. (const uint8_t *) samples,
  1063. samples_size, 1)))
  1064. return ret;
  1065. /* fabricate frame pts from sample count.
  1066. this is needed because the avcodec_encode_audio() API does not have
  1067. a way for the user to provide pts */
  1068. if(avctx->sample_rate && avctx->time_base.num)
  1069. frame->pts = ff_samples_to_time_base(avctx,
  1070. avctx->internal->sample_count);
  1071. else
  1072. frame->pts = AV_NOPTS_VALUE;
  1073. avctx->internal->sample_count += frame->nb_samples;
  1074. } else {
  1075. frame = NULL;
  1076. }
  1077. got_packet = 0;
  1078. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1079. if (!ret && got_packet && avctx->coded_frame) {
  1080. avctx->coded_frame->pts = pkt.pts;
  1081. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1082. }
  1083. /* free any side data since we cannot return it */
  1084. ff_packet_free_side_data(&pkt);
  1085. if (frame && frame->extended_data != frame->data)
  1086. av_freep(&frame->extended_data);
  1087. return ret ? ret : pkt.size;
  1088. }
  1089. #endif
  1090. #if FF_API_OLD_ENCODE_VIDEO
  1091. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1092. const AVFrame *pict)
  1093. {
  1094. AVPacket pkt;
  1095. int ret, got_packet = 0;
  1096. if(buf_size < FF_MIN_BUFFER_SIZE){
  1097. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1098. return -1;
  1099. }
  1100. av_init_packet(&pkt);
  1101. pkt.data = buf;
  1102. pkt.size = buf_size;
  1103. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1104. if (!ret && got_packet && avctx->coded_frame) {
  1105. avctx->coded_frame->pts = pkt.pts;
  1106. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1107. }
  1108. /* free any side data since we cannot return it */
  1109. if (pkt.side_data_elems > 0) {
  1110. int i;
  1111. for (i = 0; i < pkt.side_data_elems; i++)
  1112. av_free(pkt.side_data[i].data);
  1113. av_freep(&pkt.side_data);
  1114. pkt.side_data_elems = 0;
  1115. }
  1116. return ret ? ret : pkt.size;
  1117. }
  1118. #endif
  1119. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1120. AVPacket *avpkt,
  1121. const AVFrame *frame,
  1122. int *got_packet_ptr)
  1123. {
  1124. int ret;
  1125. AVPacket user_pkt = *avpkt;
  1126. int needs_realloc = !user_pkt.data;
  1127. *got_packet_ptr = 0;
  1128. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1129. av_free_packet(avpkt);
  1130. av_init_packet(avpkt);
  1131. avpkt->size = 0;
  1132. return 0;
  1133. }
  1134. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1135. return AVERROR(EINVAL);
  1136. av_assert0(avctx->codec->encode2);
  1137. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1138. av_assert0(ret <= 0);
  1139. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1140. needs_realloc = 0;
  1141. if (user_pkt.data) {
  1142. if (user_pkt.size >= avpkt->size) {
  1143. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1144. } else {
  1145. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1146. avpkt->size = user_pkt.size;
  1147. ret = -1;
  1148. }
  1149. avpkt->data = user_pkt.data;
  1150. avpkt->destruct = user_pkt.destruct;
  1151. } else {
  1152. if (av_dup_packet(avpkt) < 0) {
  1153. ret = AVERROR(ENOMEM);
  1154. }
  1155. }
  1156. }
  1157. if (!ret) {
  1158. if (!*got_packet_ptr)
  1159. avpkt->size = 0;
  1160. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1161. avpkt->pts = avpkt->dts = frame->pts;
  1162. if (needs_realloc && avpkt->data &&
  1163. avpkt->destruct == av_destruct_packet) {
  1164. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1165. if (new_data)
  1166. avpkt->data = new_data;
  1167. }
  1168. avctx->frame_number++;
  1169. }
  1170. if (ret < 0 || !*got_packet_ptr)
  1171. av_free_packet(avpkt);
  1172. emms_c();
  1173. return ret;
  1174. }
  1175. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1176. const AVSubtitle *sub)
  1177. {
  1178. int ret;
  1179. if(sub->start_display_time) {
  1180. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1181. return -1;
  1182. }
  1183. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1184. avctx->frame_number++;
  1185. return ret;
  1186. }
  1187. /**
  1188. * Attempt to guess proper monotonic timestamps for decoded video frames
  1189. * which might have incorrect times. Input timestamps may wrap around, in
  1190. * which case the output will as well.
  1191. *
  1192. * @param pts the pts field of the decoded AVPacket, as passed through
  1193. * AVFrame.pkt_pts
  1194. * @param dts the dts field of the decoded AVPacket
  1195. * @return one of the input values, may be AV_NOPTS_VALUE
  1196. */
  1197. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1198. int64_t reordered_pts, int64_t dts)
  1199. {
  1200. int64_t pts = AV_NOPTS_VALUE;
  1201. if (dts != AV_NOPTS_VALUE) {
  1202. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1203. ctx->pts_correction_last_dts = dts;
  1204. }
  1205. if (reordered_pts != AV_NOPTS_VALUE) {
  1206. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1207. ctx->pts_correction_last_pts = reordered_pts;
  1208. }
  1209. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1210. && reordered_pts != AV_NOPTS_VALUE)
  1211. pts = reordered_pts;
  1212. else
  1213. pts = dts;
  1214. return pts;
  1215. }
  1216. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1217. {
  1218. int size = 0;
  1219. const uint8_t *data;
  1220. uint32_t flags;
  1221. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1222. return;
  1223. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1224. if (!data || size < 4)
  1225. return;
  1226. flags = bytestream_get_le32(&data);
  1227. size -= 4;
  1228. if (size < 4) /* Required for any of the changes */
  1229. return;
  1230. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1231. avctx->channels = bytestream_get_le32(&data);
  1232. size -= 4;
  1233. }
  1234. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1235. if (size < 8)
  1236. return;
  1237. avctx->channel_layout = bytestream_get_le64(&data);
  1238. size -= 8;
  1239. }
  1240. if (size < 4)
  1241. return;
  1242. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1243. avctx->sample_rate = bytestream_get_le32(&data);
  1244. size -= 4;
  1245. }
  1246. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1247. if (size < 8)
  1248. return;
  1249. avctx->width = bytestream_get_le32(&data);
  1250. avctx->height = bytestream_get_le32(&data);
  1251. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1252. size -= 8;
  1253. }
  1254. }
  1255. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1256. int *got_picture_ptr,
  1257. const AVPacket *avpkt)
  1258. {
  1259. int ret;
  1260. // copy to ensure we do not change avpkt
  1261. AVPacket tmp = *avpkt;
  1262. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1263. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1264. return AVERROR(EINVAL);
  1265. }
  1266. *got_picture_ptr= 0;
  1267. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1268. return AVERROR(EINVAL);
  1269. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1270. int did_split = av_packet_split_side_data(&tmp);
  1271. apply_param_change(avctx, &tmp);
  1272. avctx->pkt = &tmp;
  1273. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1274. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1275. &tmp);
  1276. else {
  1277. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1278. &tmp);
  1279. picture->pkt_dts= avpkt->dts;
  1280. if(!avctx->has_b_frames){
  1281. picture->pkt_pos= avpkt->pos;
  1282. }
  1283. //FIXME these should be under if(!avctx->has_b_frames)
  1284. if (!picture->sample_aspect_ratio.num)
  1285. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1286. if (!picture->width)
  1287. picture->width = avctx->width;
  1288. if (!picture->height)
  1289. picture->height = avctx->height;
  1290. if (picture->format == PIX_FMT_NONE)
  1291. picture->format = avctx->pix_fmt;
  1292. }
  1293. emms_c(); //needed to avoid an emms_c() call before every return;
  1294. avctx->pkt = NULL;
  1295. if (did_split) {
  1296. ff_packet_free_side_data(&tmp);
  1297. if(ret == tmp.size)
  1298. ret = avpkt->size;
  1299. }
  1300. if (*got_picture_ptr){
  1301. avctx->frame_number++;
  1302. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1303. picture->pkt_pts,
  1304. picture->pkt_dts);
  1305. }
  1306. }else
  1307. ret= 0;
  1308. return ret;
  1309. }
  1310. #if FF_API_OLD_DECODE_AUDIO
  1311. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1312. int *frame_size_ptr,
  1313. AVPacket *avpkt)
  1314. {
  1315. AVFrame frame;
  1316. int ret, got_frame = 0;
  1317. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1318. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1319. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1320. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1321. "avcodec_decode_audio4()\n");
  1322. avctx->get_buffer = avcodec_default_get_buffer;
  1323. avctx->release_buffer = avcodec_default_release_buffer;
  1324. }
  1325. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1326. if (ret >= 0 && got_frame) {
  1327. int ch, plane_size;
  1328. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1329. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1330. frame.nb_samples,
  1331. avctx->sample_fmt, 1);
  1332. if (*frame_size_ptr < data_size) {
  1333. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1334. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1335. return AVERROR(EINVAL);
  1336. }
  1337. memcpy(samples, frame.extended_data[0], plane_size);
  1338. if (planar && avctx->channels > 1) {
  1339. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1340. for (ch = 1; ch < avctx->channels; ch++) {
  1341. memcpy(out, frame.extended_data[ch], plane_size);
  1342. out += plane_size;
  1343. }
  1344. }
  1345. *frame_size_ptr = data_size;
  1346. } else {
  1347. *frame_size_ptr = 0;
  1348. }
  1349. return ret;
  1350. }
  1351. #endif
  1352. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1353. AVFrame *frame,
  1354. int *got_frame_ptr,
  1355. const AVPacket *avpkt)
  1356. {
  1357. int ret = 0;
  1358. *got_frame_ptr = 0;
  1359. if (!avpkt->data && avpkt->size) {
  1360. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1361. return AVERROR(EINVAL);
  1362. }
  1363. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1364. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1365. return AVERROR(EINVAL);
  1366. }
  1367. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1368. // copy to ensure we do not change avpkt
  1369. AVPacket tmp = *avpkt;
  1370. int did_split = av_packet_split_side_data(&tmp);
  1371. apply_param_change(avctx, &tmp);
  1372. avctx->pkt = &tmp;
  1373. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1374. if (ret >= 0 && *got_frame_ptr) {
  1375. avctx->frame_number++;
  1376. frame->pkt_dts = avpkt->dts;
  1377. if (frame->format == AV_SAMPLE_FMT_NONE)
  1378. frame->format = avctx->sample_fmt;
  1379. if (!frame->channel_layout)
  1380. frame->channel_layout = avctx->channel_layout;
  1381. if (!frame->sample_rate)
  1382. frame->sample_rate = avctx->sample_rate;
  1383. }
  1384. avctx->pkt = NULL;
  1385. if (did_split) {
  1386. ff_packet_free_side_data(&tmp);
  1387. if(ret == tmp.size)
  1388. ret = avpkt->size;
  1389. }
  1390. }
  1391. return ret;
  1392. }
  1393. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1394. int *got_sub_ptr,
  1395. AVPacket *avpkt)
  1396. {
  1397. int ret;
  1398. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1399. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1400. return AVERROR(EINVAL);
  1401. }
  1402. avctx->pkt = avpkt;
  1403. *got_sub_ptr = 0;
  1404. avcodec_get_subtitle_defaults(sub);
  1405. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1406. if (*got_sub_ptr)
  1407. avctx->frame_number++;
  1408. return ret;
  1409. }
  1410. void avsubtitle_free(AVSubtitle *sub)
  1411. {
  1412. int i;
  1413. for (i = 0; i < sub->num_rects; i++)
  1414. {
  1415. av_freep(&sub->rects[i]->pict.data[0]);
  1416. av_freep(&sub->rects[i]->pict.data[1]);
  1417. av_freep(&sub->rects[i]->pict.data[2]);
  1418. av_freep(&sub->rects[i]->pict.data[3]);
  1419. av_freep(&sub->rects[i]->text);
  1420. av_freep(&sub->rects[i]->ass);
  1421. av_freep(&sub->rects[i]);
  1422. }
  1423. av_freep(&sub->rects);
  1424. memset(sub, 0, sizeof(AVSubtitle));
  1425. }
  1426. av_cold int avcodec_close(AVCodecContext *avctx)
  1427. {
  1428. /* If there is a user-supplied mutex locking routine, call it. */
  1429. if (ff_lockmgr_cb) {
  1430. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1431. return -1;
  1432. }
  1433. entangled_thread_counter++;
  1434. if(entangled_thread_counter != 1){
  1435. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1436. entangled_thread_counter--;
  1437. return -1;
  1438. }
  1439. if (avcodec_is_open(avctx)) {
  1440. if (HAVE_THREADS && avctx->thread_opaque)
  1441. ff_thread_free(avctx);
  1442. if (avctx->codec && avctx->codec->close)
  1443. avctx->codec->close(avctx);
  1444. avcodec_default_free_buffers(avctx);
  1445. avctx->coded_frame = NULL;
  1446. avctx->internal->byte_buffer_size = 0;
  1447. av_freep(&avctx->internal->byte_buffer);
  1448. av_freep(&avctx->internal);
  1449. }
  1450. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1451. av_opt_free(avctx->priv_data);
  1452. av_opt_free(avctx);
  1453. av_freep(&avctx->priv_data);
  1454. if (av_codec_is_encoder(avctx->codec))
  1455. av_freep(&avctx->extradata);
  1456. avctx->codec = NULL;
  1457. avctx->active_thread_type = 0;
  1458. entangled_thread_counter--;
  1459. /* Release any user-supplied mutex. */
  1460. if (ff_lockmgr_cb) {
  1461. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1462. }
  1463. return 0;
  1464. }
  1465. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1466. {
  1467. switch(id){
  1468. //This is for future deprecatec codec ids, its empty since
  1469. //last major bump but will fill up again over time, please dont remove it
  1470. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1471. default : return id;
  1472. }
  1473. }
  1474. AVCodec *avcodec_find_encoder(enum CodecID id)
  1475. {
  1476. AVCodec *p, *experimental=NULL;
  1477. p = first_avcodec;
  1478. id= remap_deprecated_codec_id(id);
  1479. while (p) {
  1480. if (av_codec_is_encoder(p) && p->id == id) {
  1481. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1482. experimental = p;
  1483. } else
  1484. return p;
  1485. }
  1486. p = p->next;
  1487. }
  1488. return experimental;
  1489. }
  1490. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1491. {
  1492. AVCodec *p;
  1493. if (!name)
  1494. return NULL;
  1495. p = first_avcodec;
  1496. while (p) {
  1497. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1498. return p;
  1499. p = p->next;
  1500. }
  1501. return NULL;
  1502. }
  1503. AVCodec *avcodec_find_decoder(enum CodecID id)
  1504. {
  1505. AVCodec *p, *experimental=NULL;
  1506. p = first_avcodec;
  1507. id= remap_deprecated_codec_id(id);
  1508. while (p) {
  1509. if (av_codec_is_decoder(p) && p->id == id) {
  1510. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1511. experimental = p;
  1512. } else
  1513. return p;
  1514. }
  1515. p = p->next;
  1516. }
  1517. return experimental;
  1518. }
  1519. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1520. {
  1521. AVCodec *p;
  1522. if (!name)
  1523. return NULL;
  1524. p = first_avcodec;
  1525. while (p) {
  1526. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1527. return p;
  1528. p = p->next;
  1529. }
  1530. return NULL;
  1531. }
  1532. const char *avcodec_get_name(enum CodecID id)
  1533. {
  1534. AVCodec *codec;
  1535. #if !CONFIG_SMALL
  1536. switch (id) {
  1537. #include "libavcodec/codec_names.h"
  1538. }
  1539. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1540. #endif
  1541. codec = avcodec_find_decoder(id);
  1542. if (codec)
  1543. return codec->name;
  1544. codec = avcodec_find_encoder(id);
  1545. if (codec)
  1546. return codec->name;
  1547. return "unknown_codec";
  1548. }
  1549. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1550. {
  1551. int i, len, ret = 0;
  1552. #define IS_PRINT(x) \
  1553. (((x) >= '0' && (x) <= '9') || \
  1554. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1555. ((x) == '.' || (x) == ' '))
  1556. for (i = 0; i < 4; i++) {
  1557. len = snprintf(buf, buf_size,
  1558. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1559. buf += len;
  1560. buf_size = buf_size > len ? buf_size - len : 0;
  1561. ret += len;
  1562. codec_tag>>=8;
  1563. }
  1564. return ret;
  1565. }
  1566. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1567. {
  1568. const char *codec_type;
  1569. const char *codec_name;
  1570. const char *profile = NULL;
  1571. AVCodec *p;
  1572. int bitrate;
  1573. AVRational display_aspect_ratio;
  1574. if (!buf || buf_size <= 0)
  1575. return;
  1576. codec_type = av_get_media_type_string(enc->codec_type);
  1577. codec_name = avcodec_get_name(enc->codec_id);
  1578. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1579. if (enc->codec)
  1580. p = enc->codec;
  1581. else
  1582. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1583. avcodec_find_decoder(enc->codec_id);
  1584. if (p)
  1585. profile = av_get_profile_name(p, enc->profile);
  1586. }
  1587. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1588. codec_name, enc->mb_decision ? " (hq)" : "");
  1589. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1590. if (profile)
  1591. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1592. if (enc->codec_tag) {
  1593. char tag_buf[32];
  1594. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1595. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1596. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1597. }
  1598. switch(enc->codec_type) {
  1599. case AVMEDIA_TYPE_VIDEO:
  1600. if (enc->pix_fmt != PIX_FMT_NONE) {
  1601. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1602. ", %s",
  1603. av_get_pix_fmt_name(enc->pix_fmt));
  1604. }
  1605. if (enc->width) {
  1606. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1607. ", %dx%d",
  1608. enc->width, enc->height);
  1609. if (enc->sample_aspect_ratio.num) {
  1610. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1611. enc->width*enc->sample_aspect_ratio.num,
  1612. enc->height*enc->sample_aspect_ratio.den,
  1613. 1024*1024);
  1614. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1615. " [SAR %d:%d DAR %d:%d]",
  1616. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1617. display_aspect_ratio.num, display_aspect_ratio.den);
  1618. }
  1619. if(av_log_get_level() >= AV_LOG_DEBUG){
  1620. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1621. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1622. ", %d/%d",
  1623. enc->time_base.num/g, enc->time_base.den/g);
  1624. }
  1625. }
  1626. if (encode) {
  1627. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1628. ", q=%d-%d", enc->qmin, enc->qmax);
  1629. }
  1630. break;
  1631. case AVMEDIA_TYPE_AUDIO:
  1632. if (enc->sample_rate) {
  1633. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1634. ", %d Hz", enc->sample_rate);
  1635. }
  1636. av_strlcat(buf, ", ", buf_size);
  1637. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1638. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1639. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1640. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1641. }
  1642. break;
  1643. default:
  1644. return;
  1645. }
  1646. if (encode) {
  1647. if (enc->flags & CODEC_FLAG_PASS1)
  1648. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1649. ", pass 1");
  1650. if (enc->flags & CODEC_FLAG_PASS2)
  1651. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1652. ", pass 2");
  1653. }
  1654. bitrate = get_bit_rate(enc);
  1655. if (bitrate != 0) {
  1656. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1657. ", %d kb/s", bitrate / 1000);
  1658. }
  1659. }
  1660. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1661. {
  1662. const AVProfile *p;
  1663. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1664. return NULL;
  1665. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1666. if (p->profile == profile)
  1667. return p->name;
  1668. return NULL;
  1669. }
  1670. unsigned avcodec_version( void )
  1671. {
  1672. // av_assert0(CODEC_ID_V410==164);
  1673. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1674. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1675. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1676. av_assert0(CODEC_ID_SRT==94216);
  1677. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1678. return LIBAVCODEC_VERSION_INT;
  1679. }
  1680. const char *avcodec_configuration(void)
  1681. {
  1682. return FFMPEG_CONFIGURATION;
  1683. }
  1684. const char *avcodec_license(void)
  1685. {
  1686. #define LICENSE_PREFIX "libavcodec license: "
  1687. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1688. }
  1689. void avcodec_flush_buffers(AVCodecContext *avctx)
  1690. {
  1691. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1692. ff_thread_flush(avctx);
  1693. else if(avctx->codec->flush)
  1694. avctx->codec->flush(avctx);
  1695. avctx->pts_correction_last_pts =
  1696. avctx->pts_correction_last_dts = INT64_MIN;
  1697. }
  1698. static void video_free_buffers(AVCodecContext *s)
  1699. {
  1700. AVCodecInternal *avci = s->internal;
  1701. int i, j;
  1702. if (!avci->buffer)
  1703. return;
  1704. if (avci->buffer_count)
  1705. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1706. avci->buffer_count);
  1707. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1708. InternalBuffer *buf = &avci->buffer[i];
  1709. for(j=0; j<4; j++){
  1710. av_freep(&buf->base[j]);
  1711. buf->data[j]= NULL;
  1712. }
  1713. }
  1714. av_freep(&avci->buffer);
  1715. avci->buffer_count=0;
  1716. }
  1717. static void audio_free_buffers(AVCodecContext *avctx)
  1718. {
  1719. AVCodecInternal *avci = avctx->internal;
  1720. InternalBuffer *buf;
  1721. if (!avci->buffer)
  1722. return;
  1723. buf = avci->buffer;
  1724. if (buf->extended_data) {
  1725. av_free(buf->extended_data[0]);
  1726. if (buf->extended_data != buf->data)
  1727. av_freep(&buf->extended_data);
  1728. }
  1729. av_freep(&avci->buffer);
  1730. }
  1731. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1732. {
  1733. switch (avctx->codec_type) {
  1734. case AVMEDIA_TYPE_VIDEO:
  1735. video_free_buffers(avctx);
  1736. break;
  1737. case AVMEDIA_TYPE_AUDIO:
  1738. audio_free_buffers(avctx);
  1739. break;
  1740. default:
  1741. break;
  1742. }
  1743. }
  1744. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1745. {
  1746. switch(codec_id){
  1747. case CODEC_ID_ADPCM_CT:
  1748. case CODEC_ID_ADPCM_IMA_APC:
  1749. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1750. case CODEC_ID_ADPCM_IMA_WS:
  1751. case CODEC_ID_ADPCM_G722:
  1752. case CODEC_ID_ADPCM_YAMAHA:
  1753. return 4;
  1754. case CODEC_ID_PCM_ALAW:
  1755. case CODEC_ID_PCM_MULAW:
  1756. case CODEC_ID_PCM_S8:
  1757. case CODEC_ID_PCM_U8:
  1758. case CODEC_ID_PCM_ZORK:
  1759. return 8;
  1760. case CODEC_ID_PCM_S16BE:
  1761. case CODEC_ID_PCM_S16LE:
  1762. case CODEC_ID_PCM_S16LE_PLANAR:
  1763. case CODEC_ID_PCM_U16BE:
  1764. case CODEC_ID_PCM_U16LE:
  1765. return 16;
  1766. case CODEC_ID_PCM_S24DAUD:
  1767. case CODEC_ID_PCM_S24BE:
  1768. case CODEC_ID_PCM_S24LE:
  1769. case CODEC_ID_PCM_U24BE:
  1770. case CODEC_ID_PCM_U24LE:
  1771. return 24;
  1772. case CODEC_ID_PCM_S32BE:
  1773. case CODEC_ID_PCM_S32LE:
  1774. case CODEC_ID_PCM_U32BE:
  1775. case CODEC_ID_PCM_U32LE:
  1776. case CODEC_ID_PCM_F32BE:
  1777. case CODEC_ID_PCM_F32LE:
  1778. return 32;
  1779. case CODEC_ID_PCM_F64BE:
  1780. case CODEC_ID_PCM_F64LE:
  1781. return 64;
  1782. default:
  1783. return 0;
  1784. }
  1785. }
  1786. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1787. {
  1788. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1789. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1790. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1791. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1792. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1793. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1794. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1795. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1796. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1797. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1798. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1799. };
  1800. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1801. return CODEC_ID_NONE;
  1802. if (be < 0 || be > 1)
  1803. be = AV_NE(1, 0);
  1804. return map[fmt][be];
  1805. }
  1806. int av_get_bits_per_sample(enum CodecID codec_id)
  1807. {
  1808. switch (codec_id) {
  1809. case CODEC_ID_ADPCM_SBPRO_2:
  1810. return 2;
  1811. case CODEC_ID_ADPCM_SBPRO_3:
  1812. return 3;
  1813. case CODEC_ID_ADPCM_SBPRO_4:
  1814. case CODEC_ID_ADPCM_IMA_WAV:
  1815. case CODEC_ID_ADPCM_IMA_QT:
  1816. case CODEC_ID_ADPCM_SWF:
  1817. case CODEC_ID_ADPCM_MS:
  1818. return 4;
  1819. default:
  1820. return av_get_exact_bits_per_sample(codec_id);
  1821. }
  1822. }
  1823. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1824. {
  1825. int id, sr, ch, ba, tag, bps;
  1826. id = avctx->codec_id;
  1827. sr = avctx->sample_rate;
  1828. ch = avctx->channels;
  1829. ba = avctx->block_align;
  1830. tag = avctx->codec_tag;
  1831. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1832. /* codecs with an exact constant bits per sample */
  1833. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1834. return (frame_bytes * 8LL) / (bps * ch);
  1835. bps = avctx->bits_per_coded_sample;
  1836. /* codecs with a fixed packet duration */
  1837. switch (id) {
  1838. case CODEC_ID_ADPCM_ADX: return 32;
  1839. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1840. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1841. case CODEC_ID_AMR_NB:
  1842. case CODEC_ID_GSM:
  1843. case CODEC_ID_QCELP:
  1844. case CODEC_ID_RA_144:
  1845. case CODEC_ID_RA_288: return 160;
  1846. case CODEC_ID_IMC: return 256;
  1847. case CODEC_ID_AMR_WB:
  1848. case CODEC_ID_GSM_MS: return 320;
  1849. case CODEC_ID_MP1: return 384;
  1850. case CODEC_ID_ATRAC1: return 512;
  1851. case CODEC_ID_ATRAC3: return 1024;
  1852. case CODEC_ID_MP2:
  1853. case CODEC_ID_MUSEPACK7: return 1152;
  1854. case CODEC_ID_AC3: return 1536;
  1855. }
  1856. if (sr > 0) {
  1857. /* calc from sample rate */
  1858. if (id == CODEC_ID_TTA)
  1859. return 256 * sr / 245;
  1860. if (ch > 0) {
  1861. /* calc from sample rate and channels */
  1862. if (id == CODEC_ID_BINKAUDIO_DCT)
  1863. return (480 << (sr / 22050)) / ch;
  1864. }
  1865. }
  1866. if (ba > 0) {
  1867. /* calc from block_align */
  1868. if (id == CODEC_ID_SIPR) {
  1869. switch (ba) {
  1870. case 20: return 160;
  1871. case 19: return 144;
  1872. case 29: return 288;
  1873. case 37: return 480;
  1874. }
  1875. }
  1876. }
  1877. if (frame_bytes > 0) {
  1878. /* calc from frame_bytes only */
  1879. if (id == CODEC_ID_TRUESPEECH)
  1880. return 240 * (frame_bytes / 32);
  1881. if (id == CODEC_ID_NELLYMOSER)
  1882. return 256 * (frame_bytes / 64);
  1883. if (bps > 0) {
  1884. /* calc from frame_bytes and bits_per_coded_sample */
  1885. if (id == CODEC_ID_ADPCM_G726)
  1886. return frame_bytes * 8 / bps;
  1887. }
  1888. if (ch > 0) {
  1889. /* calc from frame_bytes and channels */
  1890. switch (id) {
  1891. case CODEC_ID_ADPCM_4XM:
  1892. case CODEC_ID_ADPCM_IMA_ISS:
  1893. return (frame_bytes - 4 * ch) * 2 / ch;
  1894. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1895. return (frame_bytes - 4) * 2 / ch;
  1896. case CODEC_ID_ADPCM_IMA_AMV:
  1897. return (frame_bytes - 8) * 2 / ch;
  1898. case CODEC_ID_ADPCM_XA:
  1899. return (frame_bytes / 128) * 224 / ch;
  1900. case CODEC_ID_INTERPLAY_DPCM:
  1901. return (frame_bytes - 6 - ch) / ch;
  1902. case CODEC_ID_ROQ_DPCM:
  1903. return (frame_bytes - 8) / ch;
  1904. case CODEC_ID_XAN_DPCM:
  1905. return (frame_bytes - 2 * ch) / ch;
  1906. case CODEC_ID_MACE3:
  1907. return 3 * frame_bytes / ch;
  1908. case CODEC_ID_MACE6:
  1909. return 6 * frame_bytes / ch;
  1910. case CODEC_ID_PCM_LXF:
  1911. return 2 * (frame_bytes / (5 * ch));
  1912. }
  1913. if (tag) {
  1914. /* calc from frame_bytes, channels, and codec_tag */
  1915. if (id == CODEC_ID_SOL_DPCM) {
  1916. if (tag == 3)
  1917. return frame_bytes / ch;
  1918. else
  1919. return frame_bytes * 2 / ch;
  1920. }
  1921. }
  1922. if (ba > 0) {
  1923. /* calc from frame_bytes, channels, and block_align */
  1924. int blocks = frame_bytes / ba;
  1925. switch (avctx->codec_id) {
  1926. case CODEC_ID_ADPCM_IMA_WAV:
  1927. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1928. case CODEC_ID_ADPCM_IMA_DK3:
  1929. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1930. case CODEC_ID_ADPCM_IMA_DK4:
  1931. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1932. case CODEC_ID_ADPCM_MS:
  1933. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1934. }
  1935. }
  1936. if (bps > 0) {
  1937. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1938. switch (avctx->codec_id) {
  1939. case CODEC_ID_PCM_DVD:
  1940. if(bps<4)
  1941. return 0;
  1942. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1943. case CODEC_ID_PCM_BLURAY:
  1944. if(bps<4)
  1945. return 0;
  1946. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1947. case CODEC_ID_S302M:
  1948. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1949. }
  1950. }
  1951. }
  1952. }
  1953. return 0;
  1954. }
  1955. #if !HAVE_THREADS
  1956. int ff_thread_init(AVCodecContext *s){
  1957. return -1;
  1958. }
  1959. #endif
  1960. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1961. {
  1962. unsigned int n = 0;
  1963. while(v >= 0xff) {
  1964. *s++ = 0xff;
  1965. v -= 0xff;
  1966. n++;
  1967. }
  1968. *s = v;
  1969. n++;
  1970. return n;
  1971. }
  1972. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1973. int i;
  1974. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1975. return i;
  1976. }
  1977. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1978. {
  1979. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1980. "version to the newest one from Git. If the problem still "
  1981. "occurs, it means that your file has a feature which has not "
  1982. "been implemented.\n", feature);
  1983. if(want_sample)
  1984. av_log_ask_for_sample(avc, NULL);
  1985. }
  1986. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1987. {
  1988. va_list argument_list;
  1989. va_start(argument_list, msg);
  1990. if (msg)
  1991. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1992. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1993. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1994. "and contact the ffmpeg-devel mailing list.\n");
  1995. va_end(argument_list);
  1996. }
  1997. static AVHWAccel *first_hwaccel = NULL;
  1998. void av_register_hwaccel(AVHWAccel *hwaccel)
  1999. {
  2000. AVHWAccel **p = &first_hwaccel;
  2001. while (*p)
  2002. p = &(*p)->next;
  2003. *p = hwaccel;
  2004. hwaccel->next = NULL;
  2005. }
  2006. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2007. {
  2008. return hwaccel ? hwaccel->next : first_hwaccel;
  2009. }
  2010. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  2011. {
  2012. AVHWAccel *hwaccel=NULL;
  2013. while((hwaccel= av_hwaccel_next(hwaccel))){
  2014. if ( hwaccel->id == codec_id
  2015. && hwaccel->pix_fmt == pix_fmt)
  2016. return hwaccel;
  2017. }
  2018. return NULL;
  2019. }
  2020. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2021. {
  2022. if (ff_lockmgr_cb) {
  2023. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2024. return -1;
  2025. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2026. return -1;
  2027. }
  2028. ff_lockmgr_cb = cb;
  2029. if (ff_lockmgr_cb) {
  2030. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2031. return -1;
  2032. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2033. return -1;
  2034. }
  2035. return 0;
  2036. }
  2037. int avpriv_lock_avformat(void)
  2038. {
  2039. if (ff_lockmgr_cb) {
  2040. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2041. return -1;
  2042. }
  2043. return 0;
  2044. }
  2045. int avpriv_unlock_avformat(void)
  2046. {
  2047. if (ff_lockmgr_cb) {
  2048. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2049. return -1;
  2050. }
  2051. return 0;
  2052. }
  2053. unsigned int avpriv_toupper4(unsigned int x)
  2054. {
  2055. return toupper( x &0xFF)
  2056. + (toupper((x>>8 )&0xFF)<<8 )
  2057. + (toupper((x>>16)&0xFF)<<16)
  2058. + (toupper((x>>24)&0xFF)<<24);
  2059. }
  2060. #if !HAVE_THREADS
  2061. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2062. {
  2063. f->owner = avctx;
  2064. ff_init_buffer_info(avctx, f);
  2065. return avctx->get_buffer(avctx, f);
  2066. }
  2067. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2068. {
  2069. f->owner->release_buffer(f->owner, f);
  2070. }
  2071. void ff_thread_finish_setup(AVCodecContext *avctx)
  2072. {
  2073. }
  2074. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2075. {
  2076. }
  2077. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2078. {
  2079. }
  2080. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2081. {
  2082. return 1;
  2083. }
  2084. #endif
  2085. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2086. {
  2087. AVCodec *c= avcodec_find_decoder(codec_id);
  2088. if(!c)
  2089. c= avcodec_find_encoder(codec_id);
  2090. if(c)
  2091. return c->type;
  2092. if (codec_id <= CODEC_ID_NONE)
  2093. return AVMEDIA_TYPE_UNKNOWN;
  2094. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2095. return AVMEDIA_TYPE_VIDEO;
  2096. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2097. return AVMEDIA_TYPE_AUDIO;
  2098. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2099. return AVMEDIA_TYPE_SUBTITLE;
  2100. return AVMEDIA_TYPE_UNKNOWN;
  2101. }
  2102. int avcodec_is_open(AVCodecContext *s)
  2103. {
  2104. return !!s->internal;
  2105. }