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.

2411 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. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  765. ret = AVERROR(EINVAL);
  766. goto free_and_end;
  767. }
  768. }
  769. if (avctx->codec->supported_samplerates) {
  770. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  771. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  772. break;
  773. if (avctx->codec->supported_samplerates[i] == 0) {
  774. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  775. ret = AVERROR(EINVAL);
  776. goto free_and_end;
  777. }
  778. }
  779. if (avctx->codec->channel_layouts) {
  780. if (!avctx->channel_layout) {
  781. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  782. } else {
  783. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  784. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  785. break;
  786. if (avctx->codec->channel_layouts[i] == 0) {
  787. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  788. ret = AVERROR(EINVAL);
  789. goto free_and_end;
  790. }
  791. }
  792. }
  793. if (avctx->channel_layout && avctx->channels) {
  794. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  795. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  796. ret = AVERROR(EINVAL);
  797. goto free_and_end;
  798. }
  799. } else if (avctx->channel_layout) {
  800. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  801. }
  802. }
  803. avctx->pts_correction_num_faulty_pts =
  804. avctx->pts_correction_num_faulty_dts = 0;
  805. avctx->pts_correction_last_pts =
  806. avctx->pts_correction_last_dts = INT64_MIN;
  807. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  808. ret = avctx->codec->init(avctx);
  809. if (ret < 0) {
  810. goto free_and_end;
  811. }
  812. }
  813. ret=0;
  814. if (av_codec_is_decoder(avctx->codec)) {
  815. if (!avctx->bit_rate)
  816. avctx->bit_rate = get_bit_rate(avctx);
  817. /* validate channel layout from the decoder */
  818. if (avctx->channel_layout &&
  819. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  820. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  821. avctx->channel_layout = 0;
  822. }
  823. }
  824. end:
  825. entangled_thread_counter--;
  826. /* Release any user-supplied mutex. */
  827. if (ff_lockmgr_cb) {
  828. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  829. }
  830. if (options) {
  831. av_dict_free(options);
  832. *options = tmp;
  833. }
  834. return ret;
  835. free_and_end:
  836. av_dict_free(&tmp);
  837. av_freep(&avctx->priv_data);
  838. av_freep(&avctx->internal);
  839. avctx->codec= NULL;
  840. goto end;
  841. }
  842. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  843. {
  844. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  845. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  846. return AVERROR(EINVAL);
  847. }
  848. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  849. if (!avpkt->data || avpkt->size < size) {
  850. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  851. avpkt->data = avctx->internal->byte_buffer;
  852. avpkt->size = avctx->internal->byte_buffer_size;
  853. avpkt->destruct = NULL;
  854. }
  855. if (avpkt->data) {
  856. void *destruct = avpkt->destruct;
  857. if (avpkt->size < size) {
  858. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  859. return AVERROR(EINVAL);
  860. }
  861. av_init_packet(avpkt);
  862. avpkt->destruct = destruct;
  863. avpkt->size = size;
  864. return 0;
  865. } else {
  866. int ret = av_new_packet(avpkt, size);
  867. if (ret < 0)
  868. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  869. return ret;
  870. }
  871. }
  872. int ff_alloc_packet(AVPacket *avpkt, int size)
  873. {
  874. return ff_alloc_packet2(NULL, avpkt, size);
  875. }
  876. /**
  877. * Pad last frame with silence.
  878. */
  879. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  880. {
  881. AVFrame *frame = NULL;
  882. uint8_t *buf = NULL;
  883. int ret;
  884. if (!(frame = avcodec_alloc_frame()))
  885. return AVERROR(ENOMEM);
  886. *frame = *src;
  887. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  888. s->frame_size, s->sample_fmt, 0)) < 0)
  889. goto fail;
  890. if (!(buf = av_malloc(ret))) {
  891. ret = AVERROR(ENOMEM);
  892. goto fail;
  893. }
  894. frame->nb_samples = s->frame_size;
  895. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  896. buf, ret, 0)) < 0)
  897. goto fail;
  898. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  899. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  900. goto fail;
  901. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  902. frame->nb_samples - src->nb_samples,
  903. s->channels, s->sample_fmt)) < 0)
  904. goto fail;
  905. *dst = frame;
  906. return 0;
  907. fail:
  908. if (frame->extended_data != frame->data)
  909. av_freep(&frame->extended_data);
  910. av_freep(&buf);
  911. av_freep(&frame);
  912. return ret;
  913. }
  914. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  915. AVPacket *avpkt,
  916. const AVFrame *frame,
  917. int *got_packet_ptr)
  918. {
  919. AVFrame tmp;
  920. AVFrame *padded_frame = NULL;
  921. int ret;
  922. AVPacket user_pkt = *avpkt;
  923. int needs_realloc = !user_pkt.data;
  924. *got_packet_ptr = 0;
  925. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  926. av_free_packet(avpkt);
  927. av_init_packet(avpkt);
  928. return 0;
  929. }
  930. /* ensure that extended_data is properly set */
  931. if (frame && !frame->extended_data) {
  932. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  933. avctx->channels > AV_NUM_DATA_POINTERS) {
  934. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  935. "with more than %d channels, but extended_data is not set.\n",
  936. AV_NUM_DATA_POINTERS);
  937. return AVERROR(EINVAL);
  938. }
  939. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  940. tmp = *frame;
  941. tmp.extended_data = tmp.data;
  942. frame = &tmp;
  943. }
  944. /* check for valid frame size */
  945. if (frame) {
  946. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  947. if (frame->nb_samples > avctx->frame_size)
  948. return AVERROR(EINVAL);
  949. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  950. if (frame->nb_samples < avctx->frame_size &&
  951. !avctx->internal->last_audio_frame) {
  952. ret = pad_last_frame(avctx, &padded_frame, frame);
  953. if (ret < 0)
  954. return ret;
  955. frame = padded_frame;
  956. avctx->internal->last_audio_frame = 1;
  957. }
  958. if (frame->nb_samples != avctx->frame_size)
  959. return AVERROR(EINVAL);
  960. }
  961. }
  962. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  963. if (!ret) {
  964. if (*got_packet_ptr) {
  965. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  966. if (avpkt->pts == AV_NOPTS_VALUE)
  967. avpkt->pts = frame->pts;
  968. if (!avpkt->duration)
  969. avpkt->duration = ff_samples_to_time_base(avctx,
  970. frame->nb_samples);
  971. }
  972. avpkt->dts = avpkt->pts;
  973. } else {
  974. avpkt->size = 0;
  975. }
  976. }
  977. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  978. needs_realloc = 0;
  979. if (user_pkt.data) {
  980. if (user_pkt.size >= avpkt->size) {
  981. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  982. } else {
  983. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  984. avpkt->size = user_pkt.size;
  985. ret = -1;
  986. }
  987. avpkt->data = user_pkt.data;
  988. avpkt->destruct = user_pkt.destruct;
  989. } else {
  990. if (av_dup_packet(avpkt) < 0) {
  991. ret = AVERROR(ENOMEM);
  992. }
  993. }
  994. }
  995. if (!ret) {
  996. if (needs_realloc && avpkt->data) {
  997. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  998. if (new_data)
  999. avpkt->data = new_data;
  1000. }
  1001. avctx->frame_number++;
  1002. }
  1003. if (ret < 0 || !*got_packet_ptr) {
  1004. av_free_packet(avpkt);
  1005. av_init_packet(avpkt);
  1006. return ret;
  1007. }
  1008. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1009. this needs to be moved to the encoders, but for now we can do it
  1010. here to simplify things */
  1011. avpkt->flags |= AV_PKT_FLAG_KEY;
  1012. if (padded_frame) {
  1013. av_freep(&padded_frame->data[0]);
  1014. if (padded_frame->extended_data != padded_frame->data)
  1015. av_freep(&padded_frame->extended_data);
  1016. av_freep(&padded_frame);
  1017. }
  1018. return ret;
  1019. }
  1020. #if FF_API_OLD_DECODE_AUDIO
  1021. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1022. uint8_t *buf, int buf_size,
  1023. const short *samples)
  1024. {
  1025. AVPacket pkt;
  1026. AVFrame frame0;
  1027. AVFrame *frame;
  1028. int ret, samples_size, got_packet;
  1029. av_init_packet(&pkt);
  1030. pkt.data = buf;
  1031. pkt.size = buf_size;
  1032. if (samples) {
  1033. frame = &frame0;
  1034. avcodec_get_frame_defaults(frame);
  1035. if (avctx->frame_size) {
  1036. frame->nb_samples = avctx->frame_size;
  1037. } else {
  1038. /* if frame_size is not set, the number of samples must be
  1039. calculated from the buffer size */
  1040. int64_t nb_samples;
  1041. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1042. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1043. "support this codec\n");
  1044. return AVERROR(EINVAL);
  1045. }
  1046. nb_samples = (int64_t)buf_size * 8 /
  1047. (av_get_bits_per_sample(avctx->codec_id) *
  1048. avctx->channels);
  1049. if (nb_samples >= INT_MAX)
  1050. return AVERROR(EINVAL);
  1051. frame->nb_samples = nb_samples;
  1052. }
  1053. /* it is assumed that the samples buffer is large enough based on the
  1054. relevant parameters */
  1055. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1056. frame->nb_samples,
  1057. avctx->sample_fmt, 1);
  1058. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1059. avctx->sample_fmt,
  1060. (const uint8_t *) samples,
  1061. samples_size, 1)))
  1062. return ret;
  1063. /* fabricate frame pts from sample count.
  1064. this is needed because the avcodec_encode_audio() API does not have
  1065. a way for the user to provide pts */
  1066. if(avctx->sample_rate && avctx->time_base.num)
  1067. frame->pts = ff_samples_to_time_base(avctx,
  1068. avctx->internal->sample_count);
  1069. else
  1070. frame->pts = AV_NOPTS_VALUE;
  1071. avctx->internal->sample_count += frame->nb_samples;
  1072. } else {
  1073. frame = NULL;
  1074. }
  1075. got_packet = 0;
  1076. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1077. if (!ret && got_packet && avctx->coded_frame) {
  1078. avctx->coded_frame->pts = pkt.pts;
  1079. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1080. }
  1081. /* free any side data since we cannot return it */
  1082. ff_packet_free_side_data(&pkt);
  1083. if (frame && frame->extended_data != frame->data)
  1084. av_freep(&frame->extended_data);
  1085. return ret ? ret : pkt.size;
  1086. }
  1087. #endif
  1088. #if FF_API_OLD_ENCODE_VIDEO
  1089. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1090. const AVFrame *pict)
  1091. {
  1092. AVPacket pkt;
  1093. int ret, got_packet = 0;
  1094. if(buf_size < FF_MIN_BUFFER_SIZE){
  1095. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1096. return -1;
  1097. }
  1098. av_init_packet(&pkt);
  1099. pkt.data = buf;
  1100. pkt.size = buf_size;
  1101. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1102. if (!ret && got_packet && avctx->coded_frame) {
  1103. avctx->coded_frame->pts = pkt.pts;
  1104. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1105. }
  1106. /* free any side data since we cannot return it */
  1107. if (pkt.side_data_elems > 0) {
  1108. int i;
  1109. for (i = 0; i < pkt.side_data_elems; i++)
  1110. av_free(pkt.side_data[i].data);
  1111. av_freep(&pkt.side_data);
  1112. pkt.side_data_elems = 0;
  1113. }
  1114. return ret ? ret : pkt.size;
  1115. }
  1116. #endif
  1117. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1118. AVPacket *avpkt,
  1119. const AVFrame *frame,
  1120. int *got_packet_ptr)
  1121. {
  1122. int ret;
  1123. AVPacket user_pkt = *avpkt;
  1124. int needs_realloc = !user_pkt.data;
  1125. *got_packet_ptr = 0;
  1126. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1127. av_free_packet(avpkt);
  1128. av_init_packet(avpkt);
  1129. avpkt->size = 0;
  1130. return 0;
  1131. }
  1132. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1133. return AVERROR(EINVAL);
  1134. av_assert0(avctx->codec->encode2);
  1135. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1136. av_assert0(ret <= 0);
  1137. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1138. needs_realloc = 0;
  1139. if (user_pkt.data) {
  1140. if (user_pkt.size >= avpkt->size) {
  1141. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1142. } else {
  1143. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1144. avpkt->size = user_pkt.size;
  1145. ret = -1;
  1146. }
  1147. avpkt->data = user_pkt.data;
  1148. avpkt->destruct = user_pkt.destruct;
  1149. } else {
  1150. if (av_dup_packet(avpkt) < 0) {
  1151. ret = AVERROR(ENOMEM);
  1152. }
  1153. }
  1154. }
  1155. if (!ret) {
  1156. if (!*got_packet_ptr)
  1157. avpkt->size = 0;
  1158. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1159. avpkt->pts = avpkt->dts = frame->pts;
  1160. if (needs_realloc && avpkt->data &&
  1161. avpkt->destruct == av_destruct_packet) {
  1162. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1163. if (new_data)
  1164. avpkt->data = new_data;
  1165. }
  1166. avctx->frame_number++;
  1167. }
  1168. if (ret < 0 || !*got_packet_ptr)
  1169. av_free_packet(avpkt);
  1170. emms_c();
  1171. return ret;
  1172. }
  1173. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1174. const AVSubtitle *sub)
  1175. {
  1176. int ret;
  1177. if(sub->start_display_time) {
  1178. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1179. return -1;
  1180. }
  1181. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1182. avctx->frame_number++;
  1183. return ret;
  1184. }
  1185. /**
  1186. * Attempt to guess proper monotonic timestamps for decoded video frames
  1187. * which might have incorrect times. Input timestamps may wrap around, in
  1188. * which case the output will as well.
  1189. *
  1190. * @param pts the pts field of the decoded AVPacket, as passed through
  1191. * AVFrame.pkt_pts
  1192. * @param dts the dts field of the decoded AVPacket
  1193. * @return one of the input values, may be AV_NOPTS_VALUE
  1194. */
  1195. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1196. int64_t reordered_pts, int64_t dts)
  1197. {
  1198. int64_t pts = AV_NOPTS_VALUE;
  1199. if (dts != AV_NOPTS_VALUE) {
  1200. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1201. ctx->pts_correction_last_dts = dts;
  1202. }
  1203. if (reordered_pts != AV_NOPTS_VALUE) {
  1204. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1205. ctx->pts_correction_last_pts = reordered_pts;
  1206. }
  1207. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1208. && reordered_pts != AV_NOPTS_VALUE)
  1209. pts = reordered_pts;
  1210. else
  1211. pts = dts;
  1212. return pts;
  1213. }
  1214. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1215. {
  1216. int size = 0;
  1217. const uint8_t *data;
  1218. uint32_t flags;
  1219. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1220. return;
  1221. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1222. if (!data || size < 4)
  1223. return;
  1224. flags = bytestream_get_le32(&data);
  1225. size -= 4;
  1226. if (size < 4) /* Required for any of the changes */
  1227. return;
  1228. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1229. avctx->channels = bytestream_get_le32(&data);
  1230. size -= 4;
  1231. }
  1232. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1233. if (size < 8)
  1234. return;
  1235. avctx->channel_layout = bytestream_get_le64(&data);
  1236. size -= 8;
  1237. }
  1238. if (size < 4)
  1239. return;
  1240. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1241. avctx->sample_rate = bytestream_get_le32(&data);
  1242. size -= 4;
  1243. }
  1244. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1245. if (size < 8)
  1246. return;
  1247. avctx->width = bytestream_get_le32(&data);
  1248. avctx->height = bytestream_get_le32(&data);
  1249. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1250. size -= 8;
  1251. }
  1252. }
  1253. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1254. int *got_picture_ptr,
  1255. const AVPacket *avpkt)
  1256. {
  1257. int ret;
  1258. // copy to ensure we do not change avpkt
  1259. AVPacket tmp = *avpkt;
  1260. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1261. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1262. return AVERROR(EINVAL);
  1263. }
  1264. *got_picture_ptr= 0;
  1265. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1266. return -1;
  1267. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1268. int did_split = av_packet_split_side_data(&tmp);
  1269. apply_param_change(avctx, &tmp);
  1270. avctx->pkt = &tmp;
  1271. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1272. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1273. &tmp);
  1274. else {
  1275. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1276. &tmp);
  1277. picture->pkt_dts= avpkt->dts;
  1278. if(!avctx->has_b_frames){
  1279. picture->pkt_pos= avpkt->pos;
  1280. }
  1281. //FIXME these should be under if(!avctx->has_b_frames)
  1282. if (!picture->sample_aspect_ratio.num)
  1283. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1284. if (!picture->width)
  1285. picture->width = avctx->width;
  1286. if (!picture->height)
  1287. picture->height = avctx->height;
  1288. if (picture->format == PIX_FMT_NONE)
  1289. picture->format = avctx->pix_fmt;
  1290. }
  1291. emms_c(); //needed to avoid an emms_c() call before every return;
  1292. avctx->pkt = NULL;
  1293. if (did_split) {
  1294. ff_packet_free_side_data(&tmp);
  1295. if(ret == tmp.size)
  1296. ret = avpkt->size;
  1297. }
  1298. if (*got_picture_ptr){
  1299. avctx->frame_number++;
  1300. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1301. picture->pkt_pts,
  1302. picture->pkt_dts);
  1303. }
  1304. }else
  1305. ret= 0;
  1306. return ret;
  1307. }
  1308. #if FF_API_OLD_DECODE_AUDIO
  1309. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1310. int *frame_size_ptr,
  1311. AVPacket *avpkt)
  1312. {
  1313. AVFrame frame;
  1314. int ret, got_frame = 0;
  1315. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1316. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1317. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1318. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1319. "avcodec_decode_audio4()\n");
  1320. avctx->get_buffer = avcodec_default_get_buffer;
  1321. avctx->release_buffer = avcodec_default_release_buffer;
  1322. }
  1323. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1324. if (ret >= 0 && got_frame) {
  1325. int ch, plane_size;
  1326. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1327. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1328. frame.nb_samples,
  1329. avctx->sample_fmt, 1);
  1330. if (*frame_size_ptr < data_size) {
  1331. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1332. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1333. return AVERROR(EINVAL);
  1334. }
  1335. memcpy(samples, frame.extended_data[0], plane_size);
  1336. if (planar && avctx->channels > 1) {
  1337. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1338. for (ch = 1; ch < avctx->channels; ch++) {
  1339. memcpy(out, frame.extended_data[ch], plane_size);
  1340. out += plane_size;
  1341. }
  1342. }
  1343. *frame_size_ptr = data_size;
  1344. } else {
  1345. *frame_size_ptr = 0;
  1346. }
  1347. return ret;
  1348. }
  1349. #endif
  1350. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1351. AVFrame *frame,
  1352. int *got_frame_ptr,
  1353. const AVPacket *avpkt)
  1354. {
  1355. int ret = 0;
  1356. *got_frame_ptr = 0;
  1357. if (!avpkt->data && avpkt->size) {
  1358. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1359. return AVERROR(EINVAL);
  1360. }
  1361. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1362. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1363. return AVERROR(EINVAL);
  1364. }
  1365. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1366. // copy to ensure we do not change avpkt
  1367. AVPacket tmp = *avpkt;
  1368. int did_split = av_packet_split_side_data(&tmp);
  1369. apply_param_change(avctx, &tmp);
  1370. avctx->pkt = &tmp;
  1371. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1372. if (ret >= 0 && *got_frame_ptr) {
  1373. avctx->frame_number++;
  1374. frame->pkt_dts = avpkt->dts;
  1375. if (frame->format == AV_SAMPLE_FMT_NONE)
  1376. frame->format = avctx->sample_fmt;
  1377. if (!frame->channel_layout)
  1378. frame->channel_layout = avctx->channel_layout;
  1379. if (!frame->sample_rate)
  1380. frame->sample_rate = avctx->sample_rate;
  1381. }
  1382. avctx->pkt = NULL;
  1383. if (did_split) {
  1384. ff_packet_free_side_data(&tmp);
  1385. if(ret == tmp.size)
  1386. ret = avpkt->size;
  1387. }
  1388. }
  1389. return ret;
  1390. }
  1391. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1392. int *got_sub_ptr,
  1393. AVPacket *avpkt)
  1394. {
  1395. int ret;
  1396. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1397. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1398. return AVERROR(EINVAL);
  1399. }
  1400. avctx->pkt = avpkt;
  1401. *got_sub_ptr = 0;
  1402. avcodec_get_subtitle_defaults(sub);
  1403. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1404. if (*got_sub_ptr)
  1405. avctx->frame_number++;
  1406. return ret;
  1407. }
  1408. void avsubtitle_free(AVSubtitle *sub)
  1409. {
  1410. int i;
  1411. for (i = 0; i < sub->num_rects; i++)
  1412. {
  1413. av_freep(&sub->rects[i]->pict.data[0]);
  1414. av_freep(&sub->rects[i]->pict.data[1]);
  1415. av_freep(&sub->rects[i]->pict.data[2]);
  1416. av_freep(&sub->rects[i]->pict.data[3]);
  1417. av_freep(&sub->rects[i]->text);
  1418. av_freep(&sub->rects[i]->ass);
  1419. av_freep(&sub->rects[i]);
  1420. }
  1421. av_freep(&sub->rects);
  1422. memset(sub, 0, sizeof(AVSubtitle));
  1423. }
  1424. av_cold int avcodec_close(AVCodecContext *avctx)
  1425. {
  1426. /* If there is a user-supplied mutex locking routine, call it. */
  1427. if (ff_lockmgr_cb) {
  1428. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1429. return -1;
  1430. }
  1431. entangled_thread_counter++;
  1432. if(entangled_thread_counter != 1){
  1433. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1434. entangled_thread_counter--;
  1435. return -1;
  1436. }
  1437. if (avcodec_is_open(avctx)) {
  1438. if (HAVE_THREADS && avctx->thread_opaque)
  1439. ff_thread_free(avctx);
  1440. if (avctx->codec && avctx->codec->close)
  1441. avctx->codec->close(avctx);
  1442. avcodec_default_free_buffers(avctx);
  1443. avctx->coded_frame = NULL;
  1444. avctx->internal->byte_buffer_size = 0;
  1445. av_freep(&avctx->internal->byte_buffer);
  1446. av_freep(&avctx->internal);
  1447. }
  1448. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1449. av_opt_free(avctx->priv_data);
  1450. av_opt_free(avctx);
  1451. av_freep(&avctx->priv_data);
  1452. if (av_codec_is_encoder(avctx->codec))
  1453. av_freep(&avctx->extradata);
  1454. avctx->codec = NULL;
  1455. avctx->active_thread_type = 0;
  1456. entangled_thread_counter--;
  1457. /* Release any user-supplied mutex. */
  1458. if (ff_lockmgr_cb) {
  1459. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1460. }
  1461. return 0;
  1462. }
  1463. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1464. {
  1465. switch(id){
  1466. //This is for future deprecatec codec ids, its empty since
  1467. //last major bump but will fill up again over time, please dont remove it
  1468. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1469. default : return id;
  1470. }
  1471. }
  1472. AVCodec *avcodec_find_encoder(enum CodecID id)
  1473. {
  1474. AVCodec *p, *experimental=NULL;
  1475. p = first_avcodec;
  1476. id= remap_deprecated_codec_id(id);
  1477. while (p) {
  1478. if (av_codec_is_encoder(p) && p->id == id) {
  1479. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1480. experimental = p;
  1481. } else
  1482. return p;
  1483. }
  1484. p = p->next;
  1485. }
  1486. return experimental;
  1487. }
  1488. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1489. {
  1490. AVCodec *p;
  1491. if (!name)
  1492. return NULL;
  1493. p = first_avcodec;
  1494. while (p) {
  1495. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1496. return p;
  1497. p = p->next;
  1498. }
  1499. return NULL;
  1500. }
  1501. AVCodec *avcodec_find_decoder(enum CodecID id)
  1502. {
  1503. AVCodec *p, *experimental=NULL;
  1504. p = first_avcodec;
  1505. id= remap_deprecated_codec_id(id);
  1506. while (p) {
  1507. if (av_codec_is_decoder(p) && p->id == id) {
  1508. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1509. experimental = p;
  1510. } else
  1511. return p;
  1512. }
  1513. p = p->next;
  1514. }
  1515. return experimental;
  1516. }
  1517. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1518. {
  1519. AVCodec *p;
  1520. if (!name)
  1521. return NULL;
  1522. p = first_avcodec;
  1523. while (p) {
  1524. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1525. return p;
  1526. p = p->next;
  1527. }
  1528. return NULL;
  1529. }
  1530. const char *avcodec_get_name(enum CodecID id)
  1531. {
  1532. AVCodec *codec;
  1533. #if !CONFIG_SMALL
  1534. switch (id) {
  1535. #include "libavcodec/codec_names.h"
  1536. }
  1537. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1538. #endif
  1539. codec = avcodec_find_decoder(id);
  1540. if (codec)
  1541. return codec->name;
  1542. codec = avcodec_find_encoder(id);
  1543. if (codec)
  1544. return codec->name;
  1545. return "unknown_codec";
  1546. }
  1547. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1548. {
  1549. int i, len, ret = 0;
  1550. #define IS_PRINT(x) \
  1551. (((x) >= '0' && (x) <= '9') || \
  1552. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1553. ((x) == '.' || (x) == ' '))
  1554. for (i = 0; i < 4; i++) {
  1555. len = snprintf(buf, buf_size,
  1556. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1557. buf += len;
  1558. buf_size = buf_size > len ? buf_size - len : 0;
  1559. ret += len;
  1560. codec_tag>>=8;
  1561. }
  1562. return ret;
  1563. }
  1564. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1565. {
  1566. const char *codec_type;
  1567. const char *codec_name;
  1568. const char *profile = NULL;
  1569. AVCodec *p;
  1570. int bitrate;
  1571. AVRational display_aspect_ratio;
  1572. if (!buf || buf_size <= 0)
  1573. return;
  1574. codec_type = av_get_media_type_string(enc->codec_type);
  1575. codec_name = avcodec_get_name(enc->codec_id);
  1576. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1577. if (enc->codec)
  1578. p = enc->codec;
  1579. else
  1580. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1581. avcodec_find_decoder(enc->codec_id);
  1582. if (p)
  1583. profile = av_get_profile_name(p, enc->profile);
  1584. }
  1585. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1586. codec_name, enc->mb_decision ? " (hq)" : "");
  1587. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1588. if (profile)
  1589. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1590. if (enc->codec_tag) {
  1591. char tag_buf[32];
  1592. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1593. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1594. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1595. }
  1596. switch(enc->codec_type) {
  1597. case AVMEDIA_TYPE_VIDEO:
  1598. if (enc->pix_fmt != PIX_FMT_NONE) {
  1599. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1600. ", %s",
  1601. av_get_pix_fmt_name(enc->pix_fmt));
  1602. }
  1603. if (enc->width) {
  1604. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1605. ", %dx%d",
  1606. enc->width, enc->height);
  1607. if (enc->sample_aspect_ratio.num) {
  1608. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1609. enc->width*enc->sample_aspect_ratio.num,
  1610. enc->height*enc->sample_aspect_ratio.den,
  1611. 1024*1024);
  1612. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1613. " [SAR %d:%d DAR %d:%d]",
  1614. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1615. display_aspect_ratio.num, display_aspect_ratio.den);
  1616. }
  1617. if(av_log_get_level() >= AV_LOG_DEBUG){
  1618. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1619. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1620. ", %d/%d",
  1621. enc->time_base.num/g, enc->time_base.den/g);
  1622. }
  1623. }
  1624. if (encode) {
  1625. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1626. ", q=%d-%d", enc->qmin, enc->qmax);
  1627. }
  1628. break;
  1629. case AVMEDIA_TYPE_AUDIO:
  1630. if (enc->sample_rate) {
  1631. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1632. ", %d Hz", enc->sample_rate);
  1633. }
  1634. av_strlcat(buf, ", ", buf_size);
  1635. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1636. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1637. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1638. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1639. }
  1640. break;
  1641. default:
  1642. return;
  1643. }
  1644. if (encode) {
  1645. if (enc->flags & CODEC_FLAG_PASS1)
  1646. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1647. ", pass 1");
  1648. if (enc->flags & CODEC_FLAG_PASS2)
  1649. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1650. ", pass 2");
  1651. }
  1652. bitrate = get_bit_rate(enc);
  1653. if (bitrate != 0) {
  1654. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1655. ", %d kb/s", bitrate / 1000);
  1656. }
  1657. }
  1658. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1659. {
  1660. const AVProfile *p;
  1661. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1662. return NULL;
  1663. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1664. if (p->profile == profile)
  1665. return p->name;
  1666. return NULL;
  1667. }
  1668. unsigned avcodec_version( void )
  1669. {
  1670. // av_assert0(CODEC_ID_V410==164);
  1671. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1672. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1673. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1674. av_assert0(CODEC_ID_SRT==94216);
  1675. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1676. return LIBAVCODEC_VERSION_INT;
  1677. }
  1678. const char *avcodec_configuration(void)
  1679. {
  1680. return FFMPEG_CONFIGURATION;
  1681. }
  1682. const char *avcodec_license(void)
  1683. {
  1684. #define LICENSE_PREFIX "libavcodec license: "
  1685. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1686. }
  1687. void avcodec_flush_buffers(AVCodecContext *avctx)
  1688. {
  1689. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1690. ff_thread_flush(avctx);
  1691. else if(avctx->codec->flush)
  1692. avctx->codec->flush(avctx);
  1693. avctx->pts_correction_last_pts =
  1694. avctx->pts_correction_last_dts = INT64_MIN;
  1695. }
  1696. static void video_free_buffers(AVCodecContext *s)
  1697. {
  1698. AVCodecInternal *avci = s->internal;
  1699. int i, j;
  1700. if (!avci->buffer)
  1701. return;
  1702. if (avci->buffer_count)
  1703. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1704. avci->buffer_count);
  1705. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1706. InternalBuffer *buf = &avci->buffer[i];
  1707. for(j=0; j<4; j++){
  1708. av_freep(&buf->base[j]);
  1709. buf->data[j]= NULL;
  1710. }
  1711. }
  1712. av_freep(&avci->buffer);
  1713. avci->buffer_count=0;
  1714. }
  1715. static void audio_free_buffers(AVCodecContext *avctx)
  1716. {
  1717. AVCodecInternal *avci = avctx->internal;
  1718. InternalBuffer *buf;
  1719. if (!avci->buffer)
  1720. return;
  1721. buf = avci->buffer;
  1722. if (buf->extended_data) {
  1723. av_free(buf->extended_data[0]);
  1724. if (buf->extended_data != buf->data)
  1725. av_freep(&buf->extended_data);
  1726. }
  1727. av_freep(&avci->buffer);
  1728. }
  1729. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1730. {
  1731. switch (avctx->codec_type) {
  1732. case AVMEDIA_TYPE_VIDEO:
  1733. video_free_buffers(avctx);
  1734. break;
  1735. case AVMEDIA_TYPE_AUDIO:
  1736. audio_free_buffers(avctx);
  1737. break;
  1738. default:
  1739. break;
  1740. }
  1741. }
  1742. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1743. {
  1744. switch(codec_id){
  1745. case CODEC_ID_ADPCM_CT:
  1746. case CODEC_ID_ADPCM_IMA_APC:
  1747. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1748. case CODEC_ID_ADPCM_IMA_WS:
  1749. case CODEC_ID_ADPCM_G722:
  1750. case CODEC_ID_ADPCM_YAMAHA:
  1751. return 4;
  1752. case CODEC_ID_PCM_ALAW:
  1753. case CODEC_ID_PCM_MULAW:
  1754. case CODEC_ID_PCM_S8:
  1755. case CODEC_ID_PCM_U8:
  1756. case CODEC_ID_PCM_ZORK:
  1757. return 8;
  1758. case CODEC_ID_PCM_S16BE:
  1759. case CODEC_ID_PCM_S16LE:
  1760. case CODEC_ID_PCM_S16LE_PLANAR:
  1761. case CODEC_ID_PCM_U16BE:
  1762. case CODEC_ID_PCM_U16LE:
  1763. return 16;
  1764. case CODEC_ID_PCM_S24DAUD:
  1765. case CODEC_ID_PCM_S24BE:
  1766. case CODEC_ID_PCM_S24LE:
  1767. case CODEC_ID_PCM_U24BE:
  1768. case CODEC_ID_PCM_U24LE:
  1769. return 24;
  1770. case CODEC_ID_PCM_S32BE:
  1771. case CODEC_ID_PCM_S32LE:
  1772. case CODEC_ID_PCM_U32BE:
  1773. case CODEC_ID_PCM_U32LE:
  1774. case CODEC_ID_PCM_F32BE:
  1775. case CODEC_ID_PCM_F32LE:
  1776. return 32;
  1777. case CODEC_ID_PCM_F64BE:
  1778. case CODEC_ID_PCM_F64LE:
  1779. return 64;
  1780. default:
  1781. return 0;
  1782. }
  1783. }
  1784. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1785. {
  1786. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1787. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1788. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1789. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1790. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1791. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1792. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1793. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1794. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1795. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1796. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1797. };
  1798. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1799. return CODEC_ID_NONE;
  1800. if (be < 0 || be > 1)
  1801. be = AV_NE(1, 0);
  1802. return map[fmt][be];
  1803. }
  1804. int av_get_bits_per_sample(enum CodecID codec_id)
  1805. {
  1806. switch (codec_id) {
  1807. case CODEC_ID_ADPCM_SBPRO_2:
  1808. return 2;
  1809. case CODEC_ID_ADPCM_SBPRO_3:
  1810. return 3;
  1811. case CODEC_ID_ADPCM_SBPRO_4:
  1812. case CODEC_ID_ADPCM_IMA_WAV:
  1813. case CODEC_ID_ADPCM_IMA_QT:
  1814. case CODEC_ID_ADPCM_SWF:
  1815. case CODEC_ID_ADPCM_MS:
  1816. return 4;
  1817. default:
  1818. return av_get_exact_bits_per_sample(codec_id);
  1819. }
  1820. }
  1821. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1822. {
  1823. int id, sr, ch, ba, tag, bps;
  1824. id = avctx->codec_id;
  1825. sr = avctx->sample_rate;
  1826. ch = avctx->channels;
  1827. ba = avctx->block_align;
  1828. tag = avctx->codec_tag;
  1829. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1830. /* codecs with an exact constant bits per sample */
  1831. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1832. return (frame_bytes * 8LL) / (bps * ch);
  1833. bps = avctx->bits_per_coded_sample;
  1834. /* codecs with a fixed packet duration */
  1835. switch (id) {
  1836. case CODEC_ID_ADPCM_ADX: return 32;
  1837. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1838. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1839. case CODEC_ID_AMR_NB:
  1840. case CODEC_ID_GSM:
  1841. case CODEC_ID_QCELP:
  1842. case CODEC_ID_RA_144:
  1843. case CODEC_ID_RA_288: return 160;
  1844. case CODEC_ID_IMC: return 256;
  1845. case CODEC_ID_AMR_WB:
  1846. case CODEC_ID_GSM_MS: return 320;
  1847. case CODEC_ID_MP1: return 384;
  1848. case CODEC_ID_ATRAC1: return 512;
  1849. case CODEC_ID_ATRAC3: return 1024;
  1850. case CODEC_ID_MP2:
  1851. case CODEC_ID_MUSEPACK7: return 1152;
  1852. case CODEC_ID_AC3: return 1536;
  1853. }
  1854. if (sr > 0) {
  1855. /* calc from sample rate */
  1856. if (id == CODEC_ID_TTA)
  1857. return 256 * sr / 245;
  1858. if (ch > 0) {
  1859. /* calc from sample rate and channels */
  1860. if (id == CODEC_ID_BINKAUDIO_DCT)
  1861. return (480 << (sr / 22050)) / ch;
  1862. }
  1863. }
  1864. if (ba > 0) {
  1865. /* calc from block_align */
  1866. if (id == CODEC_ID_SIPR) {
  1867. switch (ba) {
  1868. case 20: return 160;
  1869. case 19: return 144;
  1870. case 29: return 288;
  1871. case 37: return 480;
  1872. }
  1873. }
  1874. }
  1875. if (frame_bytes > 0) {
  1876. /* calc from frame_bytes only */
  1877. if (id == CODEC_ID_TRUESPEECH)
  1878. return 240 * (frame_bytes / 32);
  1879. if (id == CODEC_ID_NELLYMOSER)
  1880. return 256 * (frame_bytes / 64);
  1881. if (bps > 0) {
  1882. /* calc from frame_bytes and bits_per_coded_sample */
  1883. if (id == CODEC_ID_ADPCM_G726)
  1884. return frame_bytes * 8 / bps;
  1885. }
  1886. if (ch > 0) {
  1887. /* calc from frame_bytes and channels */
  1888. switch (id) {
  1889. case CODEC_ID_ADPCM_4XM:
  1890. case CODEC_ID_ADPCM_IMA_ISS:
  1891. return (frame_bytes - 4 * ch) * 2 / ch;
  1892. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1893. return (frame_bytes - 4) * 2 / ch;
  1894. case CODEC_ID_ADPCM_IMA_AMV:
  1895. return (frame_bytes - 8) * 2 / ch;
  1896. case CODEC_ID_ADPCM_XA:
  1897. return (frame_bytes / 128) * 224 / ch;
  1898. case CODEC_ID_INTERPLAY_DPCM:
  1899. return (frame_bytes - 6 - ch) / ch;
  1900. case CODEC_ID_ROQ_DPCM:
  1901. return (frame_bytes - 8) / ch;
  1902. case CODEC_ID_XAN_DPCM:
  1903. return (frame_bytes - 2 * ch) / ch;
  1904. case CODEC_ID_MACE3:
  1905. return 3 * frame_bytes / ch;
  1906. case CODEC_ID_MACE6:
  1907. return 6 * frame_bytes / ch;
  1908. case CODEC_ID_PCM_LXF:
  1909. return 2 * (frame_bytes / (5 * ch));
  1910. }
  1911. if (tag) {
  1912. /* calc from frame_bytes, channels, and codec_tag */
  1913. if (id == CODEC_ID_SOL_DPCM) {
  1914. if (tag == 3)
  1915. return frame_bytes / ch;
  1916. else
  1917. return frame_bytes * 2 / ch;
  1918. }
  1919. }
  1920. if (ba > 0) {
  1921. /* calc from frame_bytes, channels, and block_align */
  1922. int blocks = frame_bytes / ba;
  1923. switch (avctx->codec_id) {
  1924. case CODEC_ID_ADPCM_IMA_WAV:
  1925. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1926. case CODEC_ID_ADPCM_IMA_DK3:
  1927. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1928. case CODEC_ID_ADPCM_IMA_DK4:
  1929. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1930. case CODEC_ID_ADPCM_MS:
  1931. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1932. }
  1933. }
  1934. if (bps > 0) {
  1935. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1936. switch (avctx->codec_id) {
  1937. case CODEC_ID_PCM_DVD:
  1938. if(bps<4)
  1939. return 0;
  1940. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1941. case CODEC_ID_PCM_BLURAY:
  1942. if(bps<4)
  1943. return 0;
  1944. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1945. case CODEC_ID_S302M:
  1946. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1947. }
  1948. }
  1949. }
  1950. }
  1951. return 0;
  1952. }
  1953. #if !HAVE_THREADS
  1954. int ff_thread_init(AVCodecContext *s){
  1955. return -1;
  1956. }
  1957. #endif
  1958. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1959. {
  1960. unsigned int n = 0;
  1961. while(v >= 0xff) {
  1962. *s++ = 0xff;
  1963. v -= 0xff;
  1964. n++;
  1965. }
  1966. *s = v;
  1967. n++;
  1968. return n;
  1969. }
  1970. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1971. int i;
  1972. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1973. return i;
  1974. }
  1975. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1976. {
  1977. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1978. "version to the newest one from Git. If the problem still "
  1979. "occurs, it means that your file has a feature which has not "
  1980. "been implemented.\n", feature);
  1981. if(want_sample)
  1982. av_log_ask_for_sample(avc, NULL);
  1983. }
  1984. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1985. {
  1986. va_list argument_list;
  1987. va_start(argument_list, msg);
  1988. if (msg)
  1989. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1990. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1991. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1992. "and contact the ffmpeg-devel mailing list.\n");
  1993. va_end(argument_list);
  1994. }
  1995. static AVHWAccel *first_hwaccel = NULL;
  1996. void av_register_hwaccel(AVHWAccel *hwaccel)
  1997. {
  1998. AVHWAccel **p = &first_hwaccel;
  1999. while (*p)
  2000. p = &(*p)->next;
  2001. *p = hwaccel;
  2002. hwaccel->next = NULL;
  2003. }
  2004. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2005. {
  2006. return hwaccel ? hwaccel->next : first_hwaccel;
  2007. }
  2008. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  2009. {
  2010. AVHWAccel *hwaccel=NULL;
  2011. while((hwaccel= av_hwaccel_next(hwaccel))){
  2012. if ( hwaccel->id == codec_id
  2013. && hwaccel->pix_fmt == pix_fmt)
  2014. return hwaccel;
  2015. }
  2016. return NULL;
  2017. }
  2018. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2019. {
  2020. if (ff_lockmgr_cb) {
  2021. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2022. return -1;
  2023. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2024. return -1;
  2025. }
  2026. ff_lockmgr_cb = cb;
  2027. if (ff_lockmgr_cb) {
  2028. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2029. return -1;
  2030. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2031. return -1;
  2032. }
  2033. return 0;
  2034. }
  2035. int avpriv_lock_avformat(void)
  2036. {
  2037. if (ff_lockmgr_cb) {
  2038. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2039. return -1;
  2040. }
  2041. return 0;
  2042. }
  2043. int avpriv_unlock_avformat(void)
  2044. {
  2045. if (ff_lockmgr_cb) {
  2046. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2047. return -1;
  2048. }
  2049. return 0;
  2050. }
  2051. unsigned int avpriv_toupper4(unsigned int x)
  2052. {
  2053. return toupper( x &0xFF)
  2054. + (toupper((x>>8 )&0xFF)<<8 )
  2055. + (toupper((x>>16)&0xFF)<<16)
  2056. + (toupper((x>>24)&0xFF)<<24);
  2057. }
  2058. #if !HAVE_THREADS
  2059. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2060. {
  2061. f->owner = avctx;
  2062. ff_init_buffer_info(avctx, f);
  2063. return avctx->get_buffer(avctx, f);
  2064. }
  2065. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2066. {
  2067. f->owner->release_buffer(f->owner, f);
  2068. }
  2069. void ff_thread_finish_setup(AVCodecContext *avctx)
  2070. {
  2071. }
  2072. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2073. {
  2074. }
  2075. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2076. {
  2077. }
  2078. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2079. {
  2080. return 1;
  2081. }
  2082. #endif
  2083. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2084. {
  2085. AVCodec *c= avcodec_find_decoder(codec_id);
  2086. if(!c)
  2087. c= avcodec_find_encoder(codec_id);
  2088. if(c)
  2089. return c->type;
  2090. if (codec_id <= CODEC_ID_NONE)
  2091. return AVMEDIA_TYPE_UNKNOWN;
  2092. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2093. return AVMEDIA_TYPE_VIDEO;
  2094. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2095. return AVMEDIA_TYPE_AUDIO;
  2096. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2097. return AVMEDIA_TYPE_SUBTITLE;
  2098. return AVMEDIA_TYPE_UNKNOWN;
  2099. }
  2100. int avcodec_is_open(AVCodecContext *s)
  2101. {
  2102. return !!s->internal;
  2103. }