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.

1806 lines
55KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "imgconvert.h"
  39. #include "thread.h"
  40. #include "audioconvert.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <limits.h>
  46. #include <float.h>
  47. static int volatile entangled_thread_counter=0;
  48. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  49. static void *codec_mutex;
  50. static void *avformat_mutex;
  51. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  52. {
  53. if(min_size < *size)
  54. return ptr;
  55. min_size= FFMAX(17*min_size/16 + 32, min_size);
  56. ptr= av_realloc(ptr, min_size);
  57. 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
  58. min_size= 0;
  59. *size= min_size;
  60. return ptr;
  61. }
  62. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  63. {
  64. void **p = ptr;
  65. if (min_size < *size)
  66. return;
  67. min_size= FFMAX(17*min_size/16 + 32, min_size);
  68. av_free(*p);
  69. *p = av_malloc(min_size);
  70. if (!*p) min_size = 0;
  71. *size= min_size;
  72. }
  73. /* encoder management */
  74. static AVCodec *first_avcodec = NULL;
  75. AVCodec *av_codec_next(AVCodec *c){
  76. if(c) return c->next;
  77. else return first_avcodec;
  78. }
  79. static void avcodec_init(void)
  80. {
  81. static int initialized = 0;
  82. if (initialized != 0)
  83. return;
  84. initialized = 1;
  85. dsputil_static_init();
  86. }
  87. static av_always_inline int codec_is_encoder(AVCodec *codec)
  88. {
  89. return codec && (codec->encode || codec->encode2);
  90. }
  91. static av_always_inline int codec_is_decoder(AVCodec *codec)
  92. {
  93. return codec && codec->decode;
  94. }
  95. void avcodec_register(AVCodec *codec)
  96. {
  97. AVCodec **p;
  98. avcodec_init();
  99. p = &first_avcodec;
  100. while (*p != NULL) p = &(*p)->next;
  101. *p = codec;
  102. codec->next = NULL;
  103. if (codec->init_static_data)
  104. codec->init_static_data(codec);
  105. }
  106. unsigned avcodec_get_edge_width(void)
  107. {
  108. return EDGE_WIDTH;
  109. }
  110. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  111. s->coded_width = width;
  112. s->coded_height= height;
  113. s->width = -((-width )>>s->lowres);
  114. s->height= -((-height)>>s->lowres);
  115. }
  116. #define INTERNAL_BUFFER_SIZE (32+1)
  117. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  118. int linesize_align[AV_NUM_DATA_POINTERS])
  119. {
  120. int i;
  121. int w_align= 1;
  122. int h_align= 1;
  123. switch(s->pix_fmt){
  124. case PIX_FMT_YUV420P:
  125. case PIX_FMT_YUYV422:
  126. case PIX_FMT_UYVY422:
  127. case PIX_FMT_YUV422P:
  128. case PIX_FMT_YUV440P:
  129. case PIX_FMT_YUV444P:
  130. case PIX_FMT_GBRP:
  131. case PIX_FMT_GRAY8:
  132. case PIX_FMT_GRAY16BE:
  133. case PIX_FMT_GRAY16LE:
  134. case PIX_FMT_YUVJ420P:
  135. case PIX_FMT_YUVJ422P:
  136. case PIX_FMT_YUVJ440P:
  137. case PIX_FMT_YUVJ444P:
  138. case PIX_FMT_YUVA420P:
  139. case PIX_FMT_YUV420P9LE:
  140. case PIX_FMT_YUV420P9BE:
  141. case PIX_FMT_YUV420P10LE:
  142. case PIX_FMT_YUV420P10BE:
  143. case PIX_FMT_YUV422P9LE:
  144. case PIX_FMT_YUV422P9BE:
  145. case PIX_FMT_YUV422P10LE:
  146. case PIX_FMT_YUV422P10BE:
  147. case PIX_FMT_YUV444P9LE:
  148. case PIX_FMT_YUV444P9BE:
  149. case PIX_FMT_YUV444P10LE:
  150. case PIX_FMT_YUV444P10BE:
  151. case PIX_FMT_GBRP9LE:
  152. case PIX_FMT_GBRP9BE:
  153. case PIX_FMT_GBRP10LE:
  154. case PIX_FMT_GBRP10BE:
  155. w_align = 16; //FIXME assume 16 pixel per macroblock
  156. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  157. break;
  158. case PIX_FMT_YUV411P:
  159. case PIX_FMT_UYYVYY411:
  160. w_align=32;
  161. h_align=8;
  162. break;
  163. case PIX_FMT_YUV410P:
  164. if(s->codec_id == CODEC_ID_SVQ1){
  165. w_align=64;
  166. h_align=64;
  167. }
  168. case PIX_FMT_RGB555:
  169. if(s->codec_id == CODEC_ID_RPZA){
  170. w_align=4;
  171. h_align=4;
  172. }
  173. case PIX_FMT_PAL8:
  174. case PIX_FMT_BGR8:
  175. case PIX_FMT_RGB8:
  176. if(s->codec_id == CODEC_ID_SMC){
  177. w_align=4;
  178. h_align=4;
  179. }
  180. break;
  181. case PIX_FMT_BGR24:
  182. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  183. w_align=4;
  184. h_align=4;
  185. }
  186. break;
  187. default:
  188. w_align= 1;
  189. h_align= 1;
  190. break;
  191. }
  192. *width = FFALIGN(*width , w_align);
  193. *height= FFALIGN(*height, h_align);
  194. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  195. *height+=2; // some of the optimized chroma MC reads one line too much
  196. // which is also done in mpeg decoders with lowres > 0
  197. for (i = 0; i < 4; i++)
  198. linesize_align[i] = STRIDE_ALIGN;
  199. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  200. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  201. //picture size unneccessarily in some cases. The solution here is not
  202. //pretty and better ideas are welcome!
  203. #if HAVE_MMX
  204. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  205. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  206. s->codec_id == CODEC_ID_VP6A) {
  207. for (i = 0; i < 4; i++)
  208. linesize_align[i] = 16;
  209. }
  210. #endif
  211. }
  212. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  213. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  214. int linesize_align[AV_NUM_DATA_POINTERS];
  215. int align;
  216. avcodec_align_dimensions2(s, width, height, linesize_align);
  217. align = FFMAX(linesize_align[0], linesize_align[3]);
  218. linesize_align[1] <<= chroma_shift;
  219. linesize_align[2] <<= chroma_shift;
  220. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  221. *width=FFALIGN(*width, align);
  222. }
  223. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  224. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  225. int buf_size, int align)
  226. {
  227. int ch, planar, needed_size, ret = 0;
  228. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  229. frame->nb_samples, sample_fmt,
  230. align);
  231. if (buf_size < needed_size)
  232. return AVERROR(EINVAL);
  233. planar = av_sample_fmt_is_planar(sample_fmt);
  234. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  235. if (!(frame->extended_data = av_mallocz(nb_channels *
  236. sizeof(*frame->extended_data))))
  237. return AVERROR(ENOMEM);
  238. } else {
  239. frame->extended_data = frame->data;
  240. }
  241. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  242. buf, nb_channels, frame->nb_samples,
  243. sample_fmt, align)) < 0) {
  244. if (frame->extended_data != frame->data)
  245. av_free(frame->extended_data);
  246. return ret;
  247. }
  248. if (frame->extended_data != frame->data) {
  249. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  250. frame->data[ch] = frame->extended_data[ch];
  251. }
  252. return ret;
  253. }
  254. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  255. {
  256. AVCodecInternal *avci = avctx->internal;
  257. InternalBuffer *buf;
  258. int buf_size, ret;
  259. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  260. frame->nb_samples, avctx->sample_fmt,
  261. 32);
  262. if (buf_size < 0)
  263. return AVERROR(EINVAL);
  264. /* allocate InternalBuffer if needed */
  265. if (!avci->buffer) {
  266. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  267. if (!avci->buffer)
  268. return AVERROR(ENOMEM);
  269. }
  270. buf = avci->buffer;
  271. /* if there is a previously-used internal buffer, check its size and
  272. channel count to see if we can reuse it */
  273. if (buf->extended_data) {
  274. /* if current buffer is too small, free it */
  275. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  276. av_free(buf->extended_data[0]);
  277. if (buf->extended_data != buf->data)
  278. av_free(&buf->extended_data);
  279. buf->extended_data = NULL;
  280. buf->data[0] = NULL;
  281. }
  282. /* if number of channels has changed, reset and/or free extended data
  283. pointers but leave data buffer in buf->data[0] for reuse */
  284. if (buf->nb_channels != avctx->channels) {
  285. if (buf->extended_data != buf->data)
  286. av_free(buf->extended_data);
  287. buf->extended_data = NULL;
  288. }
  289. }
  290. /* if there is no previous buffer or the previous buffer cannot be used
  291. as-is, allocate a new buffer and/or rearrange the channel pointers */
  292. if (!buf->extended_data) {
  293. if (!buf->data[0]) {
  294. if (!(buf->data[0] = av_mallocz(buf_size)))
  295. return AVERROR(ENOMEM);
  296. buf->audio_data_size = buf_size;
  297. }
  298. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  299. avctx->sample_fmt, buf->data[0],
  300. buf->audio_data_size, 32)))
  301. return ret;
  302. if (frame->extended_data == frame->data)
  303. buf->extended_data = buf->data;
  304. else
  305. buf->extended_data = frame->extended_data;
  306. memcpy(buf->data, frame->data, sizeof(frame->data));
  307. buf->linesize[0] = frame->linesize[0];
  308. buf->nb_channels = avctx->channels;
  309. } else {
  310. /* copy InternalBuffer info to the AVFrame */
  311. frame->extended_data = buf->extended_data;
  312. frame->linesize[0] = buf->linesize[0];
  313. memcpy(frame->data, buf->data, sizeof(frame->data));
  314. }
  315. frame->type = FF_BUFFER_TYPE_INTERNAL;
  316. if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
  317. else frame->pkt_pts = AV_NOPTS_VALUE;
  318. frame->reordered_opaque = avctx->reordered_opaque;
  319. if (avctx->debug & FF_DEBUG_BUFFERS)
  320. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  321. "internal audio buffer used\n", frame);
  322. return 0;
  323. }
  324. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  325. {
  326. int i;
  327. int w= s->width;
  328. int h= s->height;
  329. InternalBuffer *buf;
  330. AVCodecInternal *avci = s->internal;
  331. if(pic->data[0]!=NULL) {
  332. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  333. return -1;
  334. }
  335. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  336. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  337. return -1;
  338. }
  339. if(av_image_check_size(w, h, 0, s))
  340. return -1;
  341. if (!avci->buffer) {
  342. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  343. sizeof(InternalBuffer));
  344. }
  345. buf = &avci->buffer[avci->buffer_count];
  346. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  347. if(s->active_thread_type&FF_THREAD_FRAME) {
  348. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  349. return -1;
  350. }
  351. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  352. av_freep(&buf->base[i]);
  353. buf->data[i]= NULL;
  354. }
  355. }
  356. if (!buf->base[0]) {
  357. int h_chroma_shift, v_chroma_shift;
  358. int size[4] = {0};
  359. int tmpsize;
  360. int unaligned;
  361. AVPicture picture;
  362. int stride_align[AV_NUM_DATA_POINTERS];
  363. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  364. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  365. avcodec_align_dimensions2(s, &w, &h, stride_align);
  366. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  367. w+= EDGE_WIDTH*2;
  368. h+= EDGE_WIDTH*2;
  369. }
  370. do {
  371. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  372. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  373. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  374. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  375. w += w & ~(w-1);
  376. unaligned = 0;
  377. for (i=0; i<4; i++){
  378. unaligned |= picture.linesize[i] % stride_align[i];
  379. }
  380. } while (unaligned);
  381. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  382. if (tmpsize < 0)
  383. return -1;
  384. for (i=0; i<3 && picture.data[i+1]; i++)
  385. size[i] = picture.data[i+1] - picture.data[i];
  386. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  387. memset(buf->base, 0, sizeof(buf->base));
  388. memset(buf->data, 0, sizeof(buf->data));
  389. for(i=0; i<4 && size[i]; i++){
  390. const int h_shift= i==0 ? 0 : h_chroma_shift;
  391. const int v_shift= i==0 ? 0 : v_chroma_shift;
  392. buf->linesize[i]= picture.linesize[i];
  393. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  394. if(buf->base[i]==NULL) return -1;
  395. memset(buf->base[i], 128, size[i]);
  396. // no edge if EDGE EMU or not planar YUV
  397. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  398. buf->data[i] = buf->base[i];
  399. else
  400. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  401. }
  402. for (; i < AV_NUM_DATA_POINTERS; i++) {
  403. buf->base[i] = buf->data[i] = NULL;
  404. buf->linesize[i] = 0;
  405. }
  406. if(size[1] && !size[2])
  407. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  408. buf->width = s->width;
  409. buf->height = s->height;
  410. buf->pix_fmt= s->pix_fmt;
  411. }
  412. pic->type= FF_BUFFER_TYPE_INTERNAL;
  413. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  414. pic->base[i]= buf->base[i];
  415. pic->data[i]= buf->data[i];
  416. pic->linesize[i]= buf->linesize[i];
  417. }
  418. pic->extended_data = pic->data;
  419. avci->buffer_count++;
  420. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  421. else pic->pkt_pts= AV_NOPTS_VALUE;
  422. pic->reordered_opaque= s->reordered_opaque;
  423. if(s->debug&FF_DEBUG_BUFFERS)
  424. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  425. "buffers used\n", pic, avci->buffer_count);
  426. return 0;
  427. }
  428. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  429. {
  430. switch (avctx->codec_type) {
  431. case AVMEDIA_TYPE_VIDEO:
  432. return video_get_buffer(avctx, frame);
  433. case AVMEDIA_TYPE_AUDIO:
  434. return audio_get_buffer(avctx, frame);
  435. default:
  436. return -1;
  437. }
  438. }
  439. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  440. int i;
  441. InternalBuffer *buf, *last;
  442. AVCodecInternal *avci = s->internal;
  443. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  444. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  445. assert(avci->buffer_count);
  446. if (avci->buffer) {
  447. buf = NULL; /* avoids warning */
  448. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  449. buf = &avci->buffer[i];
  450. if (buf->data[0] == pic->data[0])
  451. break;
  452. }
  453. assert(i < avci->buffer_count);
  454. avci->buffer_count--;
  455. last = &avci->buffer[avci->buffer_count];
  456. if (buf != last)
  457. FFSWAP(InternalBuffer, *buf, *last);
  458. }
  459. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  460. pic->data[i]=NULL;
  461. // pic->base[i]=NULL;
  462. }
  463. //printf("R%X\n", pic->opaque);
  464. if(s->debug&FF_DEBUG_BUFFERS)
  465. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  466. "buffers used\n", pic, avci->buffer_count);
  467. }
  468. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  469. AVFrame temp_pic;
  470. int i;
  471. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  472. /* If no picture return a new buffer */
  473. if(pic->data[0] == NULL) {
  474. /* We will copy from buffer, so must be readable */
  475. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  476. return s->get_buffer(s, pic);
  477. }
  478. /* If internal buffer type return the same buffer */
  479. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  480. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  481. else pic->pkt_pts= AV_NOPTS_VALUE;
  482. pic->reordered_opaque= s->reordered_opaque;
  483. return 0;
  484. }
  485. /*
  486. * Not internal type and reget_buffer not overridden, emulate cr buffer
  487. */
  488. temp_pic = *pic;
  489. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  490. pic->data[i] = pic->base[i] = NULL;
  491. pic->opaque = NULL;
  492. /* Allocate new frame */
  493. if (s->get_buffer(s, pic))
  494. return -1;
  495. /* Copy image data from old buffer to new buffer */
  496. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  497. s->height);
  498. s->release_buffer(s, &temp_pic); // Release old frame
  499. return 0;
  500. }
  501. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  502. int i;
  503. for(i=0; i<count; i++){
  504. int r= func(c, (char*)arg + i*size);
  505. if(ret) ret[i]= r;
  506. }
  507. return 0;
  508. }
  509. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  510. int i;
  511. for(i=0; i<count; i++){
  512. int r= func(c, arg, i, 0);
  513. if(ret) ret[i]= r;
  514. }
  515. return 0;
  516. }
  517. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  518. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  519. ++fmt;
  520. return fmt[0];
  521. }
  522. void avcodec_get_frame_defaults(AVFrame *pic){
  523. memset(pic, 0, sizeof(AVFrame));
  524. pic->pts= AV_NOPTS_VALUE;
  525. pic->key_frame= 1;
  526. pic->sample_aspect_ratio = (AVRational){0, 1};
  527. pic->format = -1; /* unknown */
  528. }
  529. AVFrame *avcodec_alloc_frame(void){
  530. AVFrame *pic= av_malloc(sizeof(AVFrame));
  531. if(pic==NULL) return NULL;
  532. avcodec_get_frame_defaults(pic);
  533. return pic;
  534. }
  535. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  536. {
  537. int ret = 0;
  538. AVDictionary *tmp = NULL;
  539. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  540. return AVERROR(EINVAL);
  541. if (options)
  542. av_dict_copy(&tmp, *options, 0);
  543. /* If there is a user-supplied mutex locking routine, call it. */
  544. if (ff_lockmgr_cb) {
  545. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  546. return -1;
  547. }
  548. entangled_thread_counter++;
  549. if(entangled_thread_counter != 1){
  550. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  551. ret = -1;
  552. goto end;
  553. }
  554. if(avctx->codec || !codec) {
  555. ret = AVERROR(EINVAL);
  556. goto end;
  557. }
  558. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  559. if (!avctx->internal) {
  560. ret = AVERROR(ENOMEM);
  561. goto end;
  562. }
  563. if (codec->priv_data_size > 0) {
  564. if(!avctx->priv_data){
  565. avctx->priv_data = av_mallocz(codec->priv_data_size);
  566. if (!avctx->priv_data) {
  567. ret = AVERROR(ENOMEM);
  568. goto end;
  569. }
  570. if (codec->priv_class) {
  571. *(AVClass**)avctx->priv_data= codec->priv_class;
  572. av_opt_set_defaults(avctx->priv_data);
  573. }
  574. }
  575. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  576. goto free_and_end;
  577. } else {
  578. avctx->priv_data = NULL;
  579. }
  580. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  581. goto free_and_end;
  582. if(avctx->coded_width && avctx->coded_height)
  583. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  584. else if(avctx->width && avctx->height)
  585. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  586. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  587. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  588. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  589. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  590. avcodec_set_dimensions(avctx, 0, 0);
  591. }
  592. /* if the decoder init function was already called previously,
  593. free the already allocated subtitle_header before overwriting it */
  594. if (codec_is_decoder(codec))
  595. av_freep(&avctx->subtitle_header);
  596. #define SANE_NB_CHANNELS 128U
  597. if (avctx->channels > SANE_NB_CHANNELS) {
  598. ret = AVERROR(EINVAL);
  599. goto free_and_end;
  600. }
  601. avctx->codec = codec;
  602. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  603. avctx->codec_id == CODEC_ID_NONE) {
  604. avctx->codec_type = codec->type;
  605. avctx->codec_id = codec->id;
  606. }
  607. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  608. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  609. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  610. ret = AVERROR(EINVAL);
  611. goto free_and_end;
  612. }
  613. avctx->frame_number = 0;
  614. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  615. (!avctx->time_base.num || !avctx->time_base.den)) {
  616. avctx->time_base.num = 1;
  617. avctx->time_base.den = avctx->sample_rate;
  618. }
  619. if (HAVE_THREADS && !avctx->thread_opaque) {
  620. ret = ff_thread_init(avctx);
  621. if (ret < 0) {
  622. goto free_and_end;
  623. }
  624. }
  625. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  626. avctx->thread_count = 1;
  627. if (avctx->codec->max_lowres < avctx->lowres) {
  628. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  629. avctx->codec->max_lowres);
  630. ret = AVERROR(EINVAL);
  631. goto free_and_end;
  632. }
  633. if (codec_is_encoder(avctx->codec)) {
  634. int i;
  635. if (avctx->codec->sample_fmts) {
  636. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  637. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  638. break;
  639. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  640. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  641. ret = AVERROR(EINVAL);
  642. goto free_and_end;
  643. }
  644. }
  645. if (avctx->codec->supported_samplerates) {
  646. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  647. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  648. break;
  649. if (avctx->codec->supported_samplerates[i] == 0) {
  650. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  651. ret = AVERROR(EINVAL);
  652. goto free_and_end;
  653. }
  654. }
  655. if (avctx->codec->channel_layouts) {
  656. if (!avctx->channel_layout) {
  657. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  658. } else {
  659. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  660. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  661. break;
  662. if (avctx->codec->channel_layouts[i] == 0) {
  663. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  664. ret = AVERROR(EINVAL);
  665. goto free_and_end;
  666. }
  667. }
  668. }
  669. if (avctx->channel_layout && avctx->channels) {
  670. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  671. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  672. ret = AVERROR(EINVAL);
  673. goto free_and_end;
  674. }
  675. } else if (avctx->channel_layout) {
  676. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  677. }
  678. }
  679. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  680. ret = avctx->codec->init(avctx);
  681. if (ret < 0) {
  682. goto free_and_end;
  683. }
  684. }
  685. end:
  686. entangled_thread_counter--;
  687. /* Release any user-supplied mutex. */
  688. if (ff_lockmgr_cb) {
  689. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  690. }
  691. if (options) {
  692. av_dict_free(options);
  693. *options = tmp;
  694. }
  695. return ret;
  696. free_and_end:
  697. av_dict_free(&tmp);
  698. av_freep(&avctx->priv_data);
  699. av_freep(&avctx->internal);
  700. avctx->codec= NULL;
  701. goto end;
  702. }
  703. int ff_alloc_packet(AVPacket *avpkt, int size)
  704. {
  705. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  706. return AVERROR(EINVAL);
  707. if (avpkt->data) {
  708. uint8_t *pkt_data;
  709. int pkt_size;
  710. if (avpkt->size < size)
  711. return AVERROR(EINVAL);
  712. pkt_data = avpkt->data;
  713. pkt_size = avpkt->size;
  714. av_init_packet(avpkt);
  715. avpkt->data = pkt_data;
  716. avpkt->size = pkt_size;
  717. return 0;
  718. } else {
  719. return av_new_packet(avpkt, size);
  720. }
  721. }
  722. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  723. AVPacket *avpkt,
  724. const AVFrame *frame,
  725. int *got_packet_ptr)
  726. {
  727. int ret;
  728. int user_packet = !!avpkt->data;
  729. int nb_samples;
  730. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  731. av_init_packet(avpkt);
  732. avpkt->size = 0;
  733. return 0;
  734. }
  735. /* check for valid frame size */
  736. if (frame) {
  737. nb_samples = frame->nb_samples;
  738. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  739. if (nb_samples > avctx->frame_size)
  740. return AVERROR(EINVAL);
  741. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  742. if (nb_samples != avctx->frame_size)
  743. return AVERROR(EINVAL);
  744. }
  745. } else {
  746. nb_samples = avctx->frame_size;
  747. }
  748. if (avctx->codec->encode2) {
  749. *got_packet_ptr = 0;
  750. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  751. if (!ret && *got_packet_ptr &&
  752. !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  753. avpkt->pts = frame->pts;
  754. avpkt->duration = av_rescale_q(frame->nb_samples,
  755. (AVRational){ 1, avctx->sample_rate },
  756. avctx->time_base);
  757. }
  758. } else {
  759. /* for compatibility with encoders not supporting encode2(), we need to
  760. allocate a packet buffer if the user has not provided one or check
  761. the size otherwise */
  762. int fs_tmp = 0;
  763. int buf_size = avpkt->size;
  764. if (!user_packet) {
  765. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  766. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  767. buf_size = nb_samples * avctx->channels *
  768. av_get_bits_per_sample(avctx->codec_id) / 8;
  769. } else {
  770. /* this is a guess as to the required size.
  771. if an encoder needs more than this, it should probably
  772. implement encode2() */
  773. buf_size = 2 * avctx->frame_size * avctx->channels *
  774. av_get_bytes_per_sample(avctx->sample_fmt);
  775. buf_size += FF_MIN_BUFFER_SIZE;
  776. }
  777. }
  778. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  779. return ret;
  780. /* Encoders using AVCodec.encode() that support
  781. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  782. the smaller size when encoding the last frame.
  783. This code can be removed once all encoders supporting
  784. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  785. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  786. nb_samples < avctx->frame_size) {
  787. fs_tmp = avctx->frame_size;
  788. avctx->frame_size = nb_samples;
  789. }
  790. /* encode the frame */
  791. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  792. frame ? frame->data[0] : NULL);
  793. if (ret >= 0) {
  794. if (!ret) {
  795. /* no output. if the packet data was allocated by libavcodec,
  796. free it */
  797. if (!user_packet)
  798. av_freep(&avpkt->data);
  799. } else {
  800. if (avctx->coded_frame)
  801. avpkt->pts = avctx->coded_frame->pts;
  802. /* Set duration for final small packet. This can be removed
  803. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  804. encode2() */
  805. if (fs_tmp) {
  806. avpkt->duration = av_rescale_q(avctx->frame_size,
  807. (AVRational){ 1, avctx->sample_rate },
  808. avctx->time_base);
  809. }
  810. }
  811. avpkt->size = ret;
  812. *got_packet_ptr = (ret > 0);
  813. ret = 0;
  814. }
  815. if (fs_tmp)
  816. avctx->frame_size = fs_tmp;
  817. }
  818. if (!ret)
  819. avctx->frame_number++;
  820. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  821. this needs to be moved to the encoders, but for now we can do it
  822. here to simplify things */
  823. avpkt->flags |= AV_PKT_FLAG_KEY;
  824. return ret;
  825. }
  826. #if FF_API_OLD_DECODE_AUDIO
  827. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  828. uint8_t *buf, int buf_size,
  829. const short *samples)
  830. {
  831. AVPacket pkt;
  832. AVFrame frame0;
  833. AVFrame *frame;
  834. int ret, samples_size, got_packet;
  835. av_init_packet(&pkt);
  836. pkt.data = buf;
  837. pkt.size = buf_size;
  838. if (samples) {
  839. frame = &frame0;
  840. avcodec_get_frame_defaults(frame);
  841. if (avctx->frame_size) {
  842. frame->nb_samples = avctx->frame_size;
  843. } else {
  844. /* if frame_size is not set, the number of samples must be
  845. calculated from the buffer size */
  846. int64_t nb_samples;
  847. if (!av_get_bits_per_sample(avctx->codec_id)) {
  848. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  849. "support this codec\n");
  850. return AVERROR(EINVAL);
  851. }
  852. nb_samples = (int64_t)buf_size * 8 /
  853. (av_get_bits_per_sample(avctx->codec_id) *
  854. avctx->channels);
  855. if (nb_samples >= INT_MAX)
  856. return AVERROR(EINVAL);
  857. frame->nb_samples = nb_samples;
  858. }
  859. /* it is assumed that the samples buffer is large enough based on the
  860. relevant parameters */
  861. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  862. frame->nb_samples,
  863. avctx->sample_fmt, 1);
  864. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  865. avctx->sample_fmt,
  866. samples, samples_size, 1)))
  867. return ret;
  868. /* fabricate frame pts from sample count.
  869. this is needed because the avcodec_encode_audio() API does not have
  870. a way for the user to provide pts */
  871. frame->pts = av_rescale_q(avctx->internal->sample_count,
  872. (AVRational){ 1, avctx->sample_rate },
  873. avctx->time_base);
  874. avctx->internal->sample_count += frame->nb_samples;
  875. } else {
  876. frame = NULL;
  877. }
  878. got_packet = 0;
  879. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  880. if (!ret && got_packet && avctx->coded_frame) {
  881. avctx->coded_frame->pts = pkt.pts;
  882. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  883. }
  884. /* free any side data since we cannot return it */
  885. if (pkt.side_data_elems > 0) {
  886. int i;
  887. for (i = 0; i < pkt.side_data_elems; i++)
  888. av_free(pkt.side_data[i].data);
  889. av_freep(&pkt.side_data);
  890. pkt.side_data_elems = 0;
  891. }
  892. if (frame && frame->extended_data != frame->data)
  893. av_free(frame->extended_data);
  894. return ret ? ret : pkt.size;
  895. }
  896. #endif
  897. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  898. const AVFrame *pict)
  899. {
  900. if(buf_size < FF_MIN_BUFFER_SIZE){
  901. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  902. return -1;
  903. }
  904. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  905. return -1;
  906. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  907. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  908. avctx->frame_number++;
  909. emms_c(); //needed to avoid an emms_c() call before every return;
  910. return ret;
  911. }else
  912. return 0;
  913. }
  914. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  915. const AVSubtitle *sub)
  916. {
  917. int ret;
  918. if(sub->start_display_time) {
  919. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  920. return -1;
  921. }
  922. if(sub->num_rects == 0 || !sub->rects)
  923. return -1;
  924. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  925. avctx->frame_number++;
  926. return ret;
  927. }
  928. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  929. {
  930. int size = 0;
  931. const uint8_t *data;
  932. uint32_t flags;
  933. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  934. return;
  935. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  936. if (!data || size < 4)
  937. return;
  938. flags = bytestream_get_le32(&data);
  939. size -= 4;
  940. if (size < 4) /* Required for any of the changes */
  941. return;
  942. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  943. avctx->channels = bytestream_get_le32(&data);
  944. size -= 4;
  945. }
  946. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  947. if (size < 8)
  948. return;
  949. avctx->channel_layout = bytestream_get_le64(&data);
  950. size -= 8;
  951. }
  952. if (size < 4)
  953. return;
  954. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  955. avctx->sample_rate = bytestream_get_le32(&data);
  956. size -= 4;
  957. }
  958. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  959. if (size < 8)
  960. return;
  961. avctx->width = bytestream_get_le32(&data);
  962. avctx->height = bytestream_get_le32(&data);
  963. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  964. size -= 8;
  965. }
  966. }
  967. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  968. int *got_picture_ptr,
  969. AVPacket *avpkt)
  970. {
  971. int ret;
  972. *got_picture_ptr= 0;
  973. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  974. return -1;
  975. avctx->pkt = avpkt;
  976. apply_param_change(avctx, avpkt);
  977. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  978. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  979. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  980. avpkt);
  981. else {
  982. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  983. avpkt);
  984. picture->pkt_dts= avpkt->dts;
  985. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  986. picture->width = avctx->width;
  987. picture->height = avctx->height;
  988. picture->format = avctx->pix_fmt;
  989. }
  990. emms_c(); //needed to avoid an emms_c() call before every return;
  991. if (*got_picture_ptr)
  992. avctx->frame_number++;
  993. }else
  994. ret= 0;
  995. return ret;
  996. }
  997. #if FF_API_OLD_DECODE_AUDIO
  998. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  999. int *frame_size_ptr,
  1000. AVPacket *avpkt)
  1001. {
  1002. AVFrame frame;
  1003. int ret, got_frame = 0;
  1004. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1005. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1006. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1007. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1008. "avcodec_decode_audio4()\n");
  1009. avctx->get_buffer = avcodec_default_get_buffer;
  1010. }
  1011. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1012. if (ret >= 0 && got_frame) {
  1013. int ch, plane_size;
  1014. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1015. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1016. frame.nb_samples,
  1017. avctx->sample_fmt, 1);
  1018. if (*frame_size_ptr < data_size) {
  1019. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1020. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1021. return AVERROR(EINVAL);
  1022. }
  1023. memcpy(samples, frame.extended_data[0], plane_size);
  1024. if (planar && avctx->channels > 1) {
  1025. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1026. for (ch = 1; ch < avctx->channels; ch++) {
  1027. memcpy(out, frame.extended_data[ch], plane_size);
  1028. out += plane_size;
  1029. }
  1030. }
  1031. *frame_size_ptr = data_size;
  1032. } else {
  1033. *frame_size_ptr = 0;
  1034. }
  1035. return ret;
  1036. }
  1037. #endif
  1038. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1039. AVFrame *frame,
  1040. int *got_frame_ptr,
  1041. AVPacket *avpkt)
  1042. {
  1043. int ret = 0;
  1044. *got_frame_ptr = 0;
  1045. avctx->pkt = avpkt;
  1046. if (!avpkt->data && avpkt->size) {
  1047. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1048. return AVERROR(EINVAL);
  1049. }
  1050. apply_param_change(avctx, avpkt);
  1051. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1052. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1053. if (ret >= 0 && *got_frame_ptr) {
  1054. avctx->frame_number++;
  1055. frame->pkt_dts = avpkt->dts;
  1056. if (frame->format == AV_SAMPLE_FMT_NONE)
  1057. frame->format = avctx->sample_fmt;
  1058. }
  1059. }
  1060. return ret;
  1061. }
  1062. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1063. int *got_sub_ptr,
  1064. AVPacket *avpkt)
  1065. {
  1066. int ret;
  1067. avctx->pkt = avpkt;
  1068. *got_sub_ptr = 0;
  1069. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1070. if (*got_sub_ptr)
  1071. avctx->frame_number++;
  1072. return ret;
  1073. }
  1074. void avsubtitle_free(AVSubtitle *sub)
  1075. {
  1076. int i;
  1077. for (i = 0; i < sub->num_rects; i++)
  1078. {
  1079. av_freep(&sub->rects[i]->pict.data[0]);
  1080. av_freep(&sub->rects[i]->pict.data[1]);
  1081. av_freep(&sub->rects[i]->pict.data[2]);
  1082. av_freep(&sub->rects[i]->pict.data[3]);
  1083. av_freep(&sub->rects[i]->text);
  1084. av_freep(&sub->rects[i]->ass);
  1085. av_freep(&sub->rects[i]);
  1086. }
  1087. av_freep(&sub->rects);
  1088. memset(sub, 0, sizeof(AVSubtitle));
  1089. }
  1090. av_cold int avcodec_close(AVCodecContext *avctx)
  1091. {
  1092. /* If there is a user-supplied mutex locking routine, call it. */
  1093. if (ff_lockmgr_cb) {
  1094. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1095. return -1;
  1096. }
  1097. entangled_thread_counter++;
  1098. if(entangled_thread_counter != 1){
  1099. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1100. entangled_thread_counter--;
  1101. return -1;
  1102. }
  1103. if (HAVE_THREADS && avctx->thread_opaque)
  1104. ff_thread_free(avctx);
  1105. if (avctx->codec && avctx->codec->close)
  1106. avctx->codec->close(avctx);
  1107. avcodec_default_free_buffers(avctx);
  1108. avctx->coded_frame = NULL;
  1109. av_freep(&avctx->internal);
  1110. if (avctx->codec && avctx->codec->priv_class)
  1111. av_opt_free(avctx->priv_data);
  1112. av_opt_free(avctx);
  1113. av_freep(&avctx->priv_data);
  1114. if (codec_is_encoder(avctx->codec))
  1115. av_freep(&avctx->extradata);
  1116. avctx->codec = NULL;
  1117. avctx->active_thread_type = 0;
  1118. entangled_thread_counter--;
  1119. /* Release any user-supplied mutex. */
  1120. if (ff_lockmgr_cb) {
  1121. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1122. }
  1123. return 0;
  1124. }
  1125. AVCodec *avcodec_find_encoder(enum CodecID id)
  1126. {
  1127. AVCodec *p, *experimental=NULL;
  1128. p = first_avcodec;
  1129. while (p) {
  1130. if (codec_is_encoder(p) && p->id == id) {
  1131. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1132. experimental = p;
  1133. } else
  1134. return p;
  1135. }
  1136. p = p->next;
  1137. }
  1138. return experimental;
  1139. }
  1140. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1141. {
  1142. AVCodec *p;
  1143. if (!name)
  1144. return NULL;
  1145. p = first_avcodec;
  1146. while (p) {
  1147. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1148. return p;
  1149. p = p->next;
  1150. }
  1151. return NULL;
  1152. }
  1153. AVCodec *avcodec_find_decoder(enum CodecID id)
  1154. {
  1155. AVCodec *p;
  1156. p = first_avcodec;
  1157. while (p) {
  1158. if (codec_is_decoder(p) && p->id == id)
  1159. return p;
  1160. p = p->next;
  1161. }
  1162. return NULL;
  1163. }
  1164. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1165. {
  1166. AVCodec *p;
  1167. if (!name)
  1168. return NULL;
  1169. p = first_avcodec;
  1170. while (p) {
  1171. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1172. return p;
  1173. p = p->next;
  1174. }
  1175. return NULL;
  1176. }
  1177. static int get_bit_rate(AVCodecContext *ctx)
  1178. {
  1179. int bit_rate;
  1180. int bits_per_sample;
  1181. switch(ctx->codec_type) {
  1182. case AVMEDIA_TYPE_VIDEO:
  1183. case AVMEDIA_TYPE_DATA:
  1184. case AVMEDIA_TYPE_SUBTITLE:
  1185. case AVMEDIA_TYPE_ATTACHMENT:
  1186. bit_rate = ctx->bit_rate;
  1187. break;
  1188. case AVMEDIA_TYPE_AUDIO:
  1189. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1190. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1191. break;
  1192. default:
  1193. bit_rate = 0;
  1194. break;
  1195. }
  1196. return bit_rate;
  1197. }
  1198. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1199. {
  1200. int i, len, ret = 0;
  1201. for (i = 0; i < 4; i++) {
  1202. len = snprintf(buf, buf_size,
  1203. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1204. buf += len;
  1205. buf_size = buf_size > len ? buf_size - len : 0;
  1206. ret += len;
  1207. codec_tag>>=8;
  1208. }
  1209. return ret;
  1210. }
  1211. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1212. {
  1213. const char *codec_name;
  1214. const char *profile = NULL;
  1215. AVCodec *p;
  1216. char buf1[32];
  1217. int bitrate;
  1218. AVRational display_aspect_ratio;
  1219. if (encode)
  1220. p = avcodec_find_encoder(enc->codec_id);
  1221. else
  1222. p = avcodec_find_decoder(enc->codec_id);
  1223. if (p) {
  1224. codec_name = p->name;
  1225. profile = av_get_profile_name(p, enc->profile);
  1226. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  1227. /* fake mpeg2 transport stream codec (currently not
  1228. registered) */
  1229. codec_name = "mpeg2ts";
  1230. } else if (enc->codec_name[0] != '\0') {
  1231. codec_name = enc->codec_name;
  1232. } else {
  1233. /* output avi tags */
  1234. char tag_buf[32];
  1235. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1236. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1237. codec_name = buf1;
  1238. }
  1239. switch(enc->codec_type) {
  1240. case AVMEDIA_TYPE_VIDEO:
  1241. snprintf(buf, buf_size,
  1242. "Video: %s%s",
  1243. codec_name, enc->mb_decision ? " (hq)" : "");
  1244. if (profile)
  1245. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1246. " (%s)", profile);
  1247. if (enc->pix_fmt != PIX_FMT_NONE) {
  1248. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1249. ", %s",
  1250. av_get_pix_fmt_name(enc->pix_fmt));
  1251. }
  1252. if (enc->width) {
  1253. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1254. ", %dx%d",
  1255. enc->width, enc->height);
  1256. if (enc->sample_aspect_ratio.num) {
  1257. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1258. enc->width*enc->sample_aspect_ratio.num,
  1259. enc->height*enc->sample_aspect_ratio.den,
  1260. 1024*1024);
  1261. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1262. " [PAR %d:%d DAR %d:%d]",
  1263. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1264. display_aspect_ratio.num, display_aspect_ratio.den);
  1265. }
  1266. if(av_log_get_level() >= AV_LOG_DEBUG){
  1267. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1268. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1269. ", %d/%d",
  1270. enc->time_base.num/g, enc->time_base.den/g);
  1271. }
  1272. }
  1273. if (encode) {
  1274. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1275. ", q=%d-%d", enc->qmin, enc->qmax);
  1276. }
  1277. break;
  1278. case AVMEDIA_TYPE_AUDIO:
  1279. snprintf(buf, buf_size,
  1280. "Audio: %s",
  1281. codec_name);
  1282. if (profile)
  1283. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1284. " (%s)", profile);
  1285. if (enc->sample_rate) {
  1286. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1287. ", %d Hz", enc->sample_rate);
  1288. }
  1289. av_strlcat(buf, ", ", buf_size);
  1290. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1291. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1292. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1293. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1294. }
  1295. break;
  1296. case AVMEDIA_TYPE_DATA:
  1297. snprintf(buf, buf_size, "Data: %s", codec_name);
  1298. break;
  1299. case AVMEDIA_TYPE_SUBTITLE:
  1300. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1301. break;
  1302. case AVMEDIA_TYPE_ATTACHMENT:
  1303. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1304. break;
  1305. default:
  1306. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1307. return;
  1308. }
  1309. if (encode) {
  1310. if (enc->flags & CODEC_FLAG_PASS1)
  1311. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1312. ", pass 1");
  1313. if (enc->flags & CODEC_FLAG_PASS2)
  1314. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1315. ", pass 2");
  1316. }
  1317. bitrate = get_bit_rate(enc);
  1318. if (bitrate != 0) {
  1319. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1320. ", %d kb/s", bitrate / 1000);
  1321. }
  1322. }
  1323. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1324. {
  1325. const AVProfile *p;
  1326. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1327. return NULL;
  1328. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1329. if (p->profile == profile)
  1330. return p->name;
  1331. return NULL;
  1332. }
  1333. unsigned avcodec_version( void )
  1334. {
  1335. return LIBAVCODEC_VERSION_INT;
  1336. }
  1337. const char *avcodec_configuration(void)
  1338. {
  1339. return LIBAV_CONFIGURATION;
  1340. }
  1341. const char *avcodec_license(void)
  1342. {
  1343. #define LICENSE_PREFIX "libavcodec license: "
  1344. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1345. }
  1346. void avcodec_flush_buffers(AVCodecContext *avctx)
  1347. {
  1348. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1349. ff_thread_flush(avctx);
  1350. else if(avctx->codec->flush)
  1351. avctx->codec->flush(avctx);
  1352. }
  1353. static void video_free_buffers(AVCodecContext *s)
  1354. {
  1355. AVCodecInternal *avci = s->internal;
  1356. int i, j;
  1357. if (!avci->buffer)
  1358. return;
  1359. if (avci->buffer_count)
  1360. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1361. avci->buffer_count);
  1362. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1363. InternalBuffer *buf = &avci->buffer[i];
  1364. for(j=0; j<4; j++){
  1365. av_freep(&buf->base[j]);
  1366. buf->data[j]= NULL;
  1367. }
  1368. }
  1369. av_freep(&avci->buffer);
  1370. avci->buffer_count=0;
  1371. }
  1372. static void audio_free_buffers(AVCodecContext *avctx)
  1373. {
  1374. AVCodecInternal *avci = avctx->internal;
  1375. InternalBuffer *buf;
  1376. if (!avci->buffer)
  1377. return;
  1378. buf = avci->buffer;
  1379. if (buf->extended_data) {
  1380. av_free(buf->extended_data[0]);
  1381. if (buf->extended_data != buf->data)
  1382. av_free(buf->extended_data);
  1383. }
  1384. av_freep(&avci->buffer);
  1385. }
  1386. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1387. {
  1388. switch (avctx->codec_type) {
  1389. case AVMEDIA_TYPE_VIDEO:
  1390. video_free_buffers(avctx);
  1391. break;
  1392. case AVMEDIA_TYPE_AUDIO:
  1393. audio_free_buffers(avctx);
  1394. break;
  1395. default:
  1396. break;
  1397. }
  1398. }
  1399. int av_get_bits_per_sample(enum CodecID codec_id){
  1400. switch(codec_id){
  1401. case CODEC_ID_ADPCM_SBPRO_2:
  1402. return 2;
  1403. case CODEC_ID_ADPCM_SBPRO_3:
  1404. return 3;
  1405. case CODEC_ID_ADPCM_SBPRO_4:
  1406. case CODEC_ID_ADPCM_CT:
  1407. case CODEC_ID_ADPCM_IMA_APC:
  1408. case CODEC_ID_ADPCM_IMA_WAV:
  1409. case CODEC_ID_ADPCM_IMA_QT:
  1410. case CODEC_ID_ADPCM_SWF:
  1411. case CODEC_ID_ADPCM_MS:
  1412. case CODEC_ID_ADPCM_YAMAHA:
  1413. case CODEC_ID_ADPCM_G722:
  1414. return 4;
  1415. case CODEC_ID_PCM_ALAW:
  1416. case CODEC_ID_PCM_MULAW:
  1417. case CODEC_ID_PCM_S8:
  1418. case CODEC_ID_PCM_U8:
  1419. case CODEC_ID_PCM_ZORK:
  1420. return 8;
  1421. case CODEC_ID_PCM_S16BE:
  1422. case CODEC_ID_PCM_S16LE:
  1423. case CODEC_ID_PCM_S16LE_PLANAR:
  1424. case CODEC_ID_PCM_U16BE:
  1425. case CODEC_ID_PCM_U16LE:
  1426. return 16;
  1427. case CODEC_ID_PCM_S24DAUD:
  1428. case CODEC_ID_PCM_S24BE:
  1429. case CODEC_ID_PCM_S24LE:
  1430. case CODEC_ID_PCM_U24BE:
  1431. case CODEC_ID_PCM_U24LE:
  1432. return 24;
  1433. case CODEC_ID_PCM_S32BE:
  1434. case CODEC_ID_PCM_S32LE:
  1435. case CODEC_ID_PCM_U32BE:
  1436. case CODEC_ID_PCM_U32LE:
  1437. case CODEC_ID_PCM_F32BE:
  1438. case CODEC_ID_PCM_F32LE:
  1439. return 32;
  1440. case CODEC_ID_PCM_F64BE:
  1441. case CODEC_ID_PCM_F64LE:
  1442. return 64;
  1443. default:
  1444. return 0;
  1445. }
  1446. }
  1447. #if !HAVE_THREADS
  1448. int ff_thread_init(AVCodecContext *s){
  1449. return -1;
  1450. }
  1451. #endif
  1452. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1453. {
  1454. unsigned int n = 0;
  1455. while(v >= 0xff) {
  1456. *s++ = 0xff;
  1457. v -= 0xff;
  1458. n++;
  1459. }
  1460. *s = v;
  1461. n++;
  1462. return n;
  1463. }
  1464. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1465. int i;
  1466. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1467. return i;
  1468. }
  1469. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1470. {
  1471. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1472. "version to the newest one from Git. If the problem still "
  1473. "occurs, it means that your file has a feature which has not "
  1474. "been implemented.\n", feature);
  1475. if(want_sample)
  1476. av_log_ask_for_sample(avc, NULL);
  1477. }
  1478. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1479. {
  1480. va_list argument_list;
  1481. va_start(argument_list, msg);
  1482. if (msg)
  1483. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1484. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1485. "of this file to ftp://upload.libav.org/incoming/ "
  1486. "and contact the libav-devel mailing list.\n");
  1487. va_end(argument_list);
  1488. }
  1489. static AVHWAccel *first_hwaccel = NULL;
  1490. void av_register_hwaccel(AVHWAccel *hwaccel)
  1491. {
  1492. AVHWAccel **p = &first_hwaccel;
  1493. while (*p)
  1494. p = &(*p)->next;
  1495. *p = hwaccel;
  1496. hwaccel->next = NULL;
  1497. }
  1498. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1499. {
  1500. return hwaccel ? hwaccel->next : first_hwaccel;
  1501. }
  1502. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1503. {
  1504. AVHWAccel *hwaccel=NULL;
  1505. while((hwaccel= av_hwaccel_next(hwaccel))){
  1506. if ( hwaccel->id == codec_id
  1507. && hwaccel->pix_fmt == pix_fmt)
  1508. return hwaccel;
  1509. }
  1510. return NULL;
  1511. }
  1512. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1513. {
  1514. if (ff_lockmgr_cb) {
  1515. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1516. return -1;
  1517. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1518. return -1;
  1519. }
  1520. ff_lockmgr_cb = cb;
  1521. if (ff_lockmgr_cb) {
  1522. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1523. return -1;
  1524. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1525. return -1;
  1526. }
  1527. return 0;
  1528. }
  1529. int avpriv_lock_avformat(void)
  1530. {
  1531. if (ff_lockmgr_cb) {
  1532. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1533. return -1;
  1534. }
  1535. return 0;
  1536. }
  1537. int avpriv_unlock_avformat(void)
  1538. {
  1539. if (ff_lockmgr_cb) {
  1540. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1541. return -1;
  1542. }
  1543. return 0;
  1544. }
  1545. unsigned int avpriv_toupper4(unsigned int x)
  1546. {
  1547. return toupper( x &0xFF)
  1548. + (toupper((x>>8 )&0xFF)<<8 )
  1549. + (toupper((x>>16)&0xFF)<<16)
  1550. + (toupper((x>>24)&0xFF)<<24);
  1551. }
  1552. #if !HAVE_THREADS
  1553. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1554. {
  1555. f->owner = avctx;
  1556. return avctx->get_buffer(avctx, f);
  1557. }
  1558. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1559. {
  1560. f->owner->release_buffer(f->owner, f);
  1561. }
  1562. void ff_thread_finish_setup(AVCodecContext *avctx)
  1563. {
  1564. }
  1565. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1566. {
  1567. }
  1568. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1569. {
  1570. }
  1571. #endif
  1572. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1573. {
  1574. if (codec_id <= CODEC_ID_NONE)
  1575. return AVMEDIA_TYPE_UNKNOWN;
  1576. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1577. return AVMEDIA_TYPE_VIDEO;
  1578. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1579. return AVMEDIA_TYPE_AUDIO;
  1580. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1581. return AVMEDIA_TYPE_SUBTITLE;
  1582. return AVMEDIA_TYPE_UNKNOWN;
  1583. }