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.

1800 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 < AV_NUM_DATA_POINTERS; 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 < AV_NUM_DATA_POINTERS; 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 (HAVE_THREADS && !avctx->thread_opaque) {
  615. ret = ff_thread_init(avctx);
  616. if (ret < 0) {
  617. goto free_and_end;
  618. }
  619. }
  620. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  621. avctx->thread_count = 1;
  622. if (avctx->codec->max_lowres < avctx->lowres) {
  623. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  624. avctx->codec->max_lowres);
  625. ret = AVERROR(EINVAL);
  626. goto free_and_end;
  627. }
  628. if (codec_is_encoder(avctx->codec)) {
  629. int i;
  630. if (avctx->codec->sample_fmts) {
  631. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  632. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  633. break;
  634. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  635. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  636. ret = AVERROR(EINVAL);
  637. goto free_and_end;
  638. }
  639. }
  640. if (avctx->codec->supported_samplerates) {
  641. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  642. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  643. break;
  644. if (avctx->codec->supported_samplerates[i] == 0) {
  645. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  646. ret = AVERROR(EINVAL);
  647. goto free_and_end;
  648. }
  649. }
  650. if (avctx->codec->channel_layouts) {
  651. if (!avctx->channel_layout) {
  652. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  653. } else {
  654. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  655. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  656. break;
  657. if (avctx->codec->channel_layouts[i] == 0) {
  658. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  659. ret = AVERROR(EINVAL);
  660. goto free_and_end;
  661. }
  662. }
  663. }
  664. if (avctx->channel_layout && avctx->channels) {
  665. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  666. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  667. ret = AVERROR(EINVAL);
  668. goto free_and_end;
  669. }
  670. } else if (avctx->channel_layout) {
  671. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  672. }
  673. }
  674. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  675. ret = avctx->codec->init(avctx);
  676. if (ret < 0) {
  677. goto free_and_end;
  678. }
  679. }
  680. end:
  681. entangled_thread_counter--;
  682. /* Release any user-supplied mutex. */
  683. if (ff_lockmgr_cb) {
  684. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  685. }
  686. if (options) {
  687. av_dict_free(options);
  688. *options = tmp;
  689. }
  690. return ret;
  691. free_and_end:
  692. av_dict_free(&tmp);
  693. av_freep(&avctx->priv_data);
  694. av_freep(&avctx->internal);
  695. avctx->codec= NULL;
  696. goto end;
  697. }
  698. int ff_alloc_packet(AVPacket *avpkt, int size)
  699. {
  700. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  701. return AVERROR(EINVAL);
  702. if (avpkt->data) {
  703. uint8_t *pkt_data;
  704. int pkt_size;
  705. if (avpkt->size < size)
  706. return AVERROR(EINVAL);
  707. pkt_data = avpkt->data;
  708. pkt_size = avpkt->size;
  709. av_init_packet(avpkt);
  710. avpkt->data = pkt_data;
  711. avpkt->size = pkt_size;
  712. return 0;
  713. } else {
  714. return av_new_packet(avpkt, size);
  715. }
  716. }
  717. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  718. AVPacket *avpkt,
  719. const AVFrame *frame,
  720. int *got_packet_ptr)
  721. {
  722. int ret;
  723. int user_packet = !!avpkt->data;
  724. int nb_samples;
  725. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  726. av_init_packet(avpkt);
  727. avpkt->size = 0;
  728. return 0;
  729. }
  730. /* check for valid frame size */
  731. if (frame) {
  732. nb_samples = frame->nb_samples;
  733. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  734. if (nb_samples > avctx->frame_size)
  735. return AVERROR(EINVAL);
  736. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  737. if (nb_samples != avctx->frame_size)
  738. return AVERROR(EINVAL);
  739. }
  740. } else {
  741. nb_samples = avctx->frame_size;
  742. }
  743. if (avctx->codec->encode2) {
  744. *got_packet_ptr = 0;
  745. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  746. if (!ret && *got_packet_ptr &&
  747. !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  748. avpkt->pts = frame->pts;
  749. avpkt->duration = av_rescale_q(frame->nb_samples,
  750. (AVRational){ 1, avctx->sample_rate },
  751. avctx->time_base);
  752. }
  753. } else {
  754. /* for compatibility with encoders not supporting encode2(), we need to
  755. allocate a packet buffer if the user has not provided one or check
  756. the size otherwise */
  757. int fs_tmp = 0;
  758. int buf_size = avpkt->size;
  759. if (!user_packet) {
  760. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  761. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  762. buf_size = nb_samples * avctx->channels *
  763. av_get_bits_per_sample(avctx->codec_id) / 8;
  764. } else {
  765. /* this is a guess as to the required size.
  766. if an encoder needs more than this, it should probably
  767. implement encode2() */
  768. buf_size = 2 * avctx->frame_size * avctx->channels *
  769. av_get_bytes_per_sample(avctx->sample_fmt);
  770. buf_size += FF_MIN_BUFFER_SIZE;
  771. }
  772. }
  773. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  774. return ret;
  775. /* Encoders using AVCodec.encode() that support
  776. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  777. the smaller size when encoding the last frame.
  778. This code can be removed once all encoders supporting
  779. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  780. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  781. nb_samples < avctx->frame_size) {
  782. fs_tmp = avctx->frame_size;
  783. avctx->frame_size = nb_samples;
  784. }
  785. /* encode the frame */
  786. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  787. frame ? frame->data[0] : NULL);
  788. if (ret >= 0) {
  789. if (!ret) {
  790. /* no output. if the packet data was allocated by libavcodec,
  791. free it */
  792. if (!user_packet)
  793. av_freep(&avpkt->data);
  794. } else {
  795. if (avctx->coded_frame)
  796. avpkt->pts = avctx->coded_frame->pts;
  797. /* Set duration for final small packet. This can be removed
  798. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  799. encode2() */
  800. if (fs_tmp) {
  801. avpkt->duration = av_rescale_q(avctx->frame_size,
  802. (AVRational){ 1, avctx->sample_rate },
  803. avctx->time_base);
  804. }
  805. }
  806. avpkt->size = ret;
  807. *got_packet_ptr = (ret > 0);
  808. ret = 0;
  809. }
  810. if (fs_tmp)
  811. avctx->frame_size = fs_tmp;
  812. }
  813. if (!ret)
  814. avctx->frame_number++;
  815. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  816. this needs to be moved to the encoders, but for now we can do it
  817. here to simplify things */
  818. avpkt->flags |= AV_PKT_FLAG_KEY;
  819. return ret;
  820. }
  821. #if FF_API_OLD_DECODE_AUDIO
  822. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  823. uint8_t *buf, int buf_size,
  824. const short *samples)
  825. {
  826. AVPacket pkt;
  827. AVFrame frame0;
  828. AVFrame *frame;
  829. int ret, samples_size, got_packet;
  830. av_init_packet(&pkt);
  831. pkt.data = buf;
  832. pkt.size = buf_size;
  833. if (samples) {
  834. frame = &frame0;
  835. avcodec_get_frame_defaults(frame);
  836. if (avctx->frame_size) {
  837. frame->nb_samples = avctx->frame_size;
  838. } else {
  839. /* if frame_size is not set, the number of samples must be
  840. calculated from the buffer size */
  841. int64_t nb_samples;
  842. if (!av_get_bits_per_sample(avctx->codec_id)) {
  843. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  844. "support this codec\n");
  845. return AVERROR(EINVAL);
  846. }
  847. nb_samples = (int64_t)buf_size * 8 /
  848. (av_get_bits_per_sample(avctx->codec_id) *
  849. avctx->channels);
  850. if (nb_samples >= INT_MAX)
  851. return AVERROR(EINVAL);
  852. frame->nb_samples = nb_samples;
  853. }
  854. /* it is assumed that the samples buffer is large enough based on the
  855. relevant parameters */
  856. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  857. frame->nb_samples,
  858. avctx->sample_fmt, 1);
  859. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  860. avctx->sample_fmt,
  861. samples, samples_size, 1)))
  862. return ret;
  863. /* fabricate frame pts from sample count.
  864. this is needed because the avcodec_encode_audio() API does not have
  865. a way for the user to provide pts */
  866. frame->pts = av_rescale_q(avctx->internal->sample_count,
  867. (AVRational){ 1, avctx->sample_rate },
  868. avctx->time_base);
  869. avctx->internal->sample_count += frame->nb_samples;
  870. } else {
  871. frame = NULL;
  872. }
  873. got_packet = 0;
  874. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  875. if (!ret && got_packet && avctx->coded_frame) {
  876. avctx->coded_frame->pts = pkt.pts;
  877. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  878. }
  879. /* free any side data since we cannot return it */
  880. if (pkt.side_data_elems > 0) {
  881. int i;
  882. for (i = 0; i < pkt.side_data_elems; i++)
  883. av_free(pkt.side_data[i].data);
  884. av_freep(&pkt.side_data);
  885. pkt.side_data_elems = 0;
  886. }
  887. if (frame && frame->extended_data != frame->data)
  888. av_free(frame->extended_data);
  889. return ret ? ret : pkt.size;
  890. }
  891. #endif
  892. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  893. const AVFrame *pict)
  894. {
  895. if(buf_size < FF_MIN_BUFFER_SIZE){
  896. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  897. return -1;
  898. }
  899. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  900. return -1;
  901. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  902. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  903. avctx->frame_number++;
  904. emms_c(); //needed to avoid an emms_c() call before every return;
  905. return ret;
  906. }else
  907. return 0;
  908. }
  909. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  910. const AVSubtitle *sub)
  911. {
  912. int ret;
  913. if(sub->start_display_time) {
  914. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  915. return -1;
  916. }
  917. if(sub->num_rects == 0 || !sub->rects)
  918. return -1;
  919. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  920. avctx->frame_number++;
  921. return ret;
  922. }
  923. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  924. {
  925. int size = 0;
  926. const uint8_t *data;
  927. uint32_t flags;
  928. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  929. return;
  930. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  931. if (!data || size < 4)
  932. return;
  933. flags = bytestream_get_le32(&data);
  934. size -= 4;
  935. if (size < 4) /* Required for any of the changes */
  936. return;
  937. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  938. avctx->channels = bytestream_get_le32(&data);
  939. size -= 4;
  940. }
  941. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  942. if (size < 8)
  943. return;
  944. avctx->channel_layout = bytestream_get_le64(&data);
  945. size -= 8;
  946. }
  947. if (size < 4)
  948. return;
  949. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  950. avctx->sample_rate = bytestream_get_le32(&data);
  951. size -= 4;
  952. }
  953. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  954. if (size < 8)
  955. return;
  956. avctx->width = bytestream_get_le32(&data);
  957. avctx->height = bytestream_get_le32(&data);
  958. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  959. size -= 8;
  960. }
  961. }
  962. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  963. int *got_picture_ptr,
  964. AVPacket *avpkt)
  965. {
  966. int ret;
  967. *got_picture_ptr= 0;
  968. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  969. return -1;
  970. avctx->pkt = avpkt;
  971. apply_param_change(avctx, avpkt);
  972. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  973. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  974. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  975. avpkt);
  976. else {
  977. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  978. avpkt);
  979. picture->pkt_dts= avpkt->dts;
  980. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  981. picture->width = avctx->width;
  982. picture->height = avctx->height;
  983. picture->format = avctx->pix_fmt;
  984. }
  985. emms_c(); //needed to avoid an emms_c() call before every return;
  986. if (*got_picture_ptr)
  987. avctx->frame_number++;
  988. }else
  989. ret= 0;
  990. return ret;
  991. }
  992. #if FF_API_OLD_DECODE_AUDIO
  993. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  994. int *frame_size_ptr,
  995. AVPacket *avpkt)
  996. {
  997. AVFrame frame;
  998. int ret, got_frame = 0;
  999. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1000. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1001. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1002. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1003. "avcodec_decode_audio4()\n");
  1004. avctx->get_buffer = avcodec_default_get_buffer;
  1005. }
  1006. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1007. if (ret >= 0 && got_frame) {
  1008. int ch, plane_size;
  1009. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1010. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1011. frame.nb_samples,
  1012. avctx->sample_fmt, 1);
  1013. if (*frame_size_ptr < data_size) {
  1014. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1015. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1016. return AVERROR(EINVAL);
  1017. }
  1018. memcpy(samples, frame.extended_data[0], plane_size);
  1019. if (planar && avctx->channels > 1) {
  1020. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1021. for (ch = 1; ch < avctx->channels; ch++) {
  1022. memcpy(out, frame.extended_data[ch], plane_size);
  1023. out += plane_size;
  1024. }
  1025. }
  1026. *frame_size_ptr = data_size;
  1027. } else {
  1028. *frame_size_ptr = 0;
  1029. }
  1030. return ret;
  1031. }
  1032. #endif
  1033. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1034. AVFrame *frame,
  1035. int *got_frame_ptr,
  1036. AVPacket *avpkt)
  1037. {
  1038. int ret = 0;
  1039. *got_frame_ptr = 0;
  1040. avctx->pkt = avpkt;
  1041. if (!avpkt->data && avpkt->size) {
  1042. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1043. return AVERROR(EINVAL);
  1044. }
  1045. apply_param_change(avctx, avpkt);
  1046. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1047. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1048. if (ret >= 0 && *got_frame_ptr) {
  1049. avctx->frame_number++;
  1050. frame->pkt_dts = avpkt->dts;
  1051. if (frame->format == AV_SAMPLE_FMT_NONE)
  1052. frame->format = avctx->sample_fmt;
  1053. }
  1054. }
  1055. return ret;
  1056. }
  1057. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1058. int *got_sub_ptr,
  1059. AVPacket *avpkt)
  1060. {
  1061. int ret;
  1062. avctx->pkt = avpkt;
  1063. *got_sub_ptr = 0;
  1064. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1065. if (*got_sub_ptr)
  1066. avctx->frame_number++;
  1067. return ret;
  1068. }
  1069. void avsubtitle_free(AVSubtitle *sub)
  1070. {
  1071. int i;
  1072. for (i = 0; i < sub->num_rects; i++)
  1073. {
  1074. av_freep(&sub->rects[i]->pict.data[0]);
  1075. av_freep(&sub->rects[i]->pict.data[1]);
  1076. av_freep(&sub->rects[i]->pict.data[2]);
  1077. av_freep(&sub->rects[i]->pict.data[3]);
  1078. av_freep(&sub->rects[i]->text);
  1079. av_freep(&sub->rects[i]->ass);
  1080. av_freep(&sub->rects[i]);
  1081. }
  1082. av_freep(&sub->rects);
  1083. memset(sub, 0, sizeof(AVSubtitle));
  1084. }
  1085. av_cold int avcodec_close(AVCodecContext *avctx)
  1086. {
  1087. /* If there is a user-supplied mutex locking routine, call it. */
  1088. if (ff_lockmgr_cb) {
  1089. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1090. return -1;
  1091. }
  1092. entangled_thread_counter++;
  1093. if(entangled_thread_counter != 1){
  1094. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1095. entangled_thread_counter--;
  1096. return -1;
  1097. }
  1098. if (HAVE_THREADS && avctx->thread_opaque)
  1099. ff_thread_free(avctx);
  1100. if (avctx->codec && avctx->codec->close)
  1101. avctx->codec->close(avctx);
  1102. avcodec_default_free_buffers(avctx);
  1103. avctx->coded_frame = NULL;
  1104. av_freep(&avctx->internal);
  1105. if (avctx->codec && avctx->codec->priv_class)
  1106. av_opt_free(avctx->priv_data);
  1107. av_opt_free(avctx);
  1108. av_freep(&avctx->priv_data);
  1109. if (codec_is_encoder(avctx->codec))
  1110. av_freep(&avctx->extradata);
  1111. avctx->codec = NULL;
  1112. avctx->active_thread_type = 0;
  1113. entangled_thread_counter--;
  1114. /* Release any user-supplied mutex. */
  1115. if (ff_lockmgr_cb) {
  1116. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1117. }
  1118. return 0;
  1119. }
  1120. AVCodec *avcodec_find_encoder(enum CodecID id)
  1121. {
  1122. AVCodec *p, *experimental=NULL;
  1123. p = first_avcodec;
  1124. while (p) {
  1125. if (codec_is_encoder(p) && p->id == id) {
  1126. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1127. experimental = p;
  1128. } else
  1129. return p;
  1130. }
  1131. p = p->next;
  1132. }
  1133. return experimental;
  1134. }
  1135. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1136. {
  1137. AVCodec *p;
  1138. if (!name)
  1139. return NULL;
  1140. p = first_avcodec;
  1141. while (p) {
  1142. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1143. return p;
  1144. p = p->next;
  1145. }
  1146. return NULL;
  1147. }
  1148. AVCodec *avcodec_find_decoder(enum CodecID id)
  1149. {
  1150. AVCodec *p;
  1151. p = first_avcodec;
  1152. while (p) {
  1153. if (codec_is_decoder(p) && p->id == id)
  1154. return p;
  1155. p = p->next;
  1156. }
  1157. return NULL;
  1158. }
  1159. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1160. {
  1161. AVCodec *p;
  1162. if (!name)
  1163. return NULL;
  1164. p = first_avcodec;
  1165. while (p) {
  1166. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1167. return p;
  1168. p = p->next;
  1169. }
  1170. return NULL;
  1171. }
  1172. static int get_bit_rate(AVCodecContext *ctx)
  1173. {
  1174. int bit_rate;
  1175. int bits_per_sample;
  1176. switch(ctx->codec_type) {
  1177. case AVMEDIA_TYPE_VIDEO:
  1178. case AVMEDIA_TYPE_DATA:
  1179. case AVMEDIA_TYPE_SUBTITLE:
  1180. case AVMEDIA_TYPE_ATTACHMENT:
  1181. bit_rate = ctx->bit_rate;
  1182. break;
  1183. case AVMEDIA_TYPE_AUDIO:
  1184. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1185. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1186. break;
  1187. default:
  1188. bit_rate = 0;
  1189. break;
  1190. }
  1191. return bit_rate;
  1192. }
  1193. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1194. {
  1195. int i, len, ret = 0;
  1196. for (i = 0; i < 4; i++) {
  1197. len = snprintf(buf, buf_size,
  1198. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1199. buf += len;
  1200. buf_size = buf_size > len ? buf_size - len : 0;
  1201. ret += len;
  1202. codec_tag>>=8;
  1203. }
  1204. return ret;
  1205. }
  1206. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1207. {
  1208. const char *codec_name;
  1209. const char *profile = NULL;
  1210. AVCodec *p;
  1211. char buf1[32];
  1212. int bitrate;
  1213. AVRational display_aspect_ratio;
  1214. if (encode)
  1215. p = avcodec_find_encoder(enc->codec_id);
  1216. else
  1217. p = avcodec_find_decoder(enc->codec_id);
  1218. if (p) {
  1219. codec_name = p->name;
  1220. profile = av_get_profile_name(p, enc->profile);
  1221. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  1222. /* fake mpeg2 transport stream codec (currently not
  1223. registered) */
  1224. codec_name = "mpeg2ts";
  1225. } else if (enc->codec_name[0] != '\0') {
  1226. codec_name = enc->codec_name;
  1227. } else {
  1228. /* output avi tags */
  1229. char tag_buf[32];
  1230. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1231. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1232. codec_name = buf1;
  1233. }
  1234. switch(enc->codec_type) {
  1235. case AVMEDIA_TYPE_VIDEO:
  1236. snprintf(buf, buf_size,
  1237. "Video: %s%s",
  1238. codec_name, enc->mb_decision ? " (hq)" : "");
  1239. if (profile)
  1240. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1241. " (%s)", profile);
  1242. if (enc->pix_fmt != PIX_FMT_NONE) {
  1243. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1244. ", %s",
  1245. av_get_pix_fmt_name(enc->pix_fmt));
  1246. }
  1247. if (enc->width) {
  1248. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1249. ", %dx%d",
  1250. enc->width, enc->height);
  1251. if (enc->sample_aspect_ratio.num) {
  1252. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1253. enc->width*enc->sample_aspect_ratio.num,
  1254. enc->height*enc->sample_aspect_ratio.den,
  1255. 1024*1024);
  1256. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1257. " [PAR %d:%d DAR %d:%d]",
  1258. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1259. display_aspect_ratio.num, display_aspect_ratio.den);
  1260. }
  1261. if(av_log_get_level() >= AV_LOG_DEBUG){
  1262. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1263. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1264. ", %d/%d",
  1265. enc->time_base.num/g, enc->time_base.den/g);
  1266. }
  1267. }
  1268. if (encode) {
  1269. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1270. ", q=%d-%d", enc->qmin, enc->qmax);
  1271. }
  1272. break;
  1273. case AVMEDIA_TYPE_AUDIO:
  1274. snprintf(buf, buf_size,
  1275. "Audio: %s",
  1276. codec_name);
  1277. if (profile)
  1278. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1279. " (%s)", profile);
  1280. if (enc->sample_rate) {
  1281. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1282. ", %d Hz", enc->sample_rate);
  1283. }
  1284. av_strlcat(buf, ", ", buf_size);
  1285. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1286. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1287. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1288. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1289. }
  1290. break;
  1291. case AVMEDIA_TYPE_DATA:
  1292. snprintf(buf, buf_size, "Data: %s", codec_name);
  1293. break;
  1294. case AVMEDIA_TYPE_SUBTITLE:
  1295. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1296. break;
  1297. case AVMEDIA_TYPE_ATTACHMENT:
  1298. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1299. break;
  1300. default:
  1301. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1302. return;
  1303. }
  1304. if (encode) {
  1305. if (enc->flags & CODEC_FLAG_PASS1)
  1306. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1307. ", pass 1");
  1308. if (enc->flags & CODEC_FLAG_PASS2)
  1309. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1310. ", pass 2");
  1311. }
  1312. bitrate = get_bit_rate(enc);
  1313. if (bitrate != 0) {
  1314. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1315. ", %d kb/s", bitrate / 1000);
  1316. }
  1317. }
  1318. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1319. {
  1320. const AVProfile *p;
  1321. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1322. return NULL;
  1323. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1324. if (p->profile == profile)
  1325. return p->name;
  1326. return NULL;
  1327. }
  1328. unsigned avcodec_version( void )
  1329. {
  1330. return LIBAVCODEC_VERSION_INT;
  1331. }
  1332. const char *avcodec_configuration(void)
  1333. {
  1334. return LIBAV_CONFIGURATION;
  1335. }
  1336. const char *avcodec_license(void)
  1337. {
  1338. #define LICENSE_PREFIX "libavcodec license: "
  1339. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1340. }
  1341. void avcodec_flush_buffers(AVCodecContext *avctx)
  1342. {
  1343. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1344. ff_thread_flush(avctx);
  1345. else if(avctx->codec->flush)
  1346. avctx->codec->flush(avctx);
  1347. }
  1348. static void video_free_buffers(AVCodecContext *s)
  1349. {
  1350. AVCodecInternal *avci = s->internal;
  1351. int i, j;
  1352. if (!avci->buffer)
  1353. return;
  1354. if (avci->buffer_count)
  1355. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1356. avci->buffer_count);
  1357. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1358. InternalBuffer *buf = &avci->buffer[i];
  1359. for(j=0; j<4; j++){
  1360. av_freep(&buf->base[j]);
  1361. buf->data[j]= NULL;
  1362. }
  1363. }
  1364. av_freep(&avci->buffer);
  1365. avci->buffer_count=0;
  1366. }
  1367. static void audio_free_buffers(AVCodecContext *avctx)
  1368. {
  1369. AVCodecInternal *avci = avctx->internal;
  1370. InternalBuffer *buf;
  1371. if (!avci->buffer)
  1372. return;
  1373. buf = avci->buffer;
  1374. if (buf->extended_data) {
  1375. av_free(buf->extended_data[0]);
  1376. if (buf->extended_data != buf->data)
  1377. av_free(buf->extended_data);
  1378. }
  1379. av_freep(&avci->buffer);
  1380. }
  1381. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1382. {
  1383. switch (avctx->codec_type) {
  1384. case AVMEDIA_TYPE_VIDEO:
  1385. video_free_buffers(avctx);
  1386. break;
  1387. case AVMEDIA_TYPE_AUDIO:
  1388. audio_free_buffers(avctx);
  1389. break;
  1390. default:
  1391. break;
  1392. }
  1393. }
  1394. int av_get_bits_per_sample(enum CodecID codec_id){
  1395. switch(codec_id){
  1396. case CODEC_ID_ADPCM_SBPRO_2:
  1397. return 2;
  1398. case CODEC_ID_ADPCM_SBPRO_3:
  1399. return 3;
  1400. case CODEC_ID_ADPCM_SBPRO_4:
  1401. case CODEC_ID_ADPCM_CT:
  1402. case CODEC_ID_ADPCM_IMA_APC:
  1403. case CODEC_ID_ADPCM_IMA_WAV:
  1404. case CODEC_ID_ADPCM_IMA_QT:
  1405. case CODEC_ID_ADPCM_SWF:
  1406. case CODEC_ID_ADPCM_MS:
  1407. case CODEC_ID_ADPCM_YAMAHA:
  1408. case CODEC_ID_ADPCM_G722:
  1409. return 4;
  1410. case CODEC_ID_PCM_ALAW:
  1411. case CODEC_ID_PCM_MULAW:
  1412. case CODEC_ID_PCM_S8:
  1413. case CODEC_ID_PCM_U8:
  1414. case CODEC_ID_PCM_ZORK:
  1415. return 8;
  1416. case CODEC_ID_PCM_S16BE:
  1417. case CODEC_ID_PCM_S16LE:
  1418. case CODEC_ID_PCM_S16LE_PLANAR:
  1419. case CODEC_ID_PCM_U16BE:
  1420. case CODEC_ID_PCM_U16LE:
  1421. return 16;
  1422. case CODEC_ID_PCM_S24DAUD:
  1423. case CODEC_ID_PCM_S24BE:
  1424. case CODEC_ID_PCM_S24LE:
  1425. case CODEC_ID_PCM_U24BE:
  1426. case CODEC_ID_PCM_U24LE:
  1427. return 24;
  1428. case CODEC_ID_PCM_S32BE:
  1429. case CODEC_ID_PCM_S32LE:
  1430. case CODEC_ID_PCM_U32BE:
  1431. case CODEC_ID_PCM_U32LE:
  1432. case CODEC_ID_PCM_F32BE:
  1433. case CODEC_ID_PCM_F32LE:
  1434. return 32;
  1435. case CODEC_ID_PCM_F64BE:
  1436. case CODEC_ID_PCM_F64LE:
  1437. return 64;
  1438. default:
  1439. return 0;
  1440. }
  1441. }
  1442. #if !HAVE_THREADS
  1443. int ff_thread_init(AVCodecContext *s){
  1444. return -1;
  1445. }
  1446. #endif
  1447. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1448. {
  1449. unsigned int n = 0;
  1450. while(v >= 0xff) {
  1451. *s++ = 0xff;
  1452. v -= 0xff;
  1453. n++;
  1454. }
  1455. *s = v;
  1456. n++;
  1457. return n;
  1458. }
  1459. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1460. int i;
  1461. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1462. return i;
  1463. }
  1464. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1465. {
  1466. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1467. "version to the newest one from Git. If the problem still "
  1468. "occurs, it means that your file has a feature which has not "
  1469. "been implemented.\n", feature);
  1470. if(want_sample)
  1471. av_log_ask_for_sample(avc, NULL);
  1472. }
  1473. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1474. {
  1475. va_list argument_list;
  1476. va_start(argument_list, msg);
  1477. if (msg)
  1478. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1479. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1480. "of this file to ftp://upload.libav.org/incoming/ "
  1481. "and contact the libav-devel mailing list.\n");
  1482. va_end(argument_list);
  1483. }
  1484. static AVHWAccel *first_hwaccel = NULL;
  1485. void av_register_hwaccel(AVHWAccel *hwaccel)
  1486. {
  1487. AVHWAccel **p = &first_hwaccel;
  1488. while (*p)
  1489. p = &(*p)->next;
  1490. *p = hwaccel;
  1491. hwaccel->next = NULL;
  1492. }
  1493. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1494. {
  1495. return hwaccel ? hwaccel->next : first_hwaccel;
  1496. }
  1497. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1498. {
  1499. AVHWAccel *hwaccel=NULL;
  1500. while((hwaccel= av_hwaccel_next(hwaccel))){
  1501. if ( hwaccel->id == codec_id
  1502. && hwaccel->pix_fmt == pix_fmt)
  1503. return hwaccel;
  1504. }
  1505. return NULL;
  1506. }
  1507. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1508. {
  1509. if (ff_lockmgr_cb) {
  1510. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1511. return -1;
  1512. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1513. return -1;
  1514. }
  1515. ff_lockmgr_cb = cb;
  1516. if (ff_lockmgr_cb) {
  1517. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1518. return -1;
  1519. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1520. return -1;
  1521. }
  1522. return 0;
  1523. }
  1524. int avpriv_lock_avformat(void)
  1525. {
  1526. if (ff_lockmgr_cb) {
  1527. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1528. return -1;
  1529. }
  1530. return 0;
  1531. }
  1532. int avpriv_unlock_avformat(void)
  1533. {
  1534. if (ff_lockmgr_cb) {
  1535. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1536. return -1;
  1537. }
  1538. return 0;
  1539. }
  1540. unsigned int avpriv_toupper4(unsigned int x)
  1541. {
  1542. return toupper( x &0xFF)
  1543. + (toupper((x>>8 )&0xFF)<<8 )
  1544. + (toupper((x>>16)&0xFF)<<16)
  1545. + (toupper((x>>24)&0xFF)<<24);
  1546. }
  1547. #if !HAVE_THREADS
  1548. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1549. {
  1550. f->owner = avctx;
  1551. return avctx->get_buffer(avctx, f);
  1552. }
  1553. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1554. {
  1555. f->owner->release_buffer(f->owner, f);
  1556. }
  1557. void ff_thread_finish_setup(AVCodecContext *avctx)
  1558. {
  1559. }
  1560. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1561. {
  1562. }
  1563. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1564. {
  1565. }
  1566. #endif
  1567. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1568. {
  1569. if (codec_id <= CODEC_ID_NONE)
  1570. return AVMEDIA_TYPE_UNKNOWN;
  1571. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1572. return AVMEDIA_TYPE_VIDEO;
  1573. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1574. return AVMEDIA_TYPE_AUDIO;
  1575. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1576. return AVMEDIA_TYPE_SUBTITLE;
  1577. return AVMEDIA_TYPE_UNKNOWN;
  1578. }