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.

1487 lines
44KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. static void *avformat_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. min_size= FFMAX(17*min_size/16 + 32, min_size);
  54. ptr= av_realloc(ptr, min_size);
  55. 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
  56. min_size= 0;
  57. *size= min_size;
  58. return ptr;
  59. }
  60. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  61. {
  62. void **p = ptr;
  63. if (min_size < *size)
  64. return;
  65. min_size= FFMAX(17*min_size/16 + 32, min_size);
  66. av_free(*p);
  67. *p = av_malloc(min_size);
  68. if (!*p) min_size = 0;
  69. *size= min_size;
  70. }
  71. /* encoder management */
  72. static AVCodec *first_avcodec = NULL;
  73. AVCodec *av_codec_next(AVCodec *c){
  74. if(c) return c->next;
  75. else return first_avcodec;
  76. }
  77. #if !FF_API_AVCODEC_INIT
  78. static
  79. #endif
  80. void avcodec_init(void)
  81. {
  82. static int initialized = 0;
  83. if (initialized != 0)
  84. return;
  85. initialized = 1;
  86. dsputil_static_init();
  87. }
  88. void avcodec_register(AVCodec *codec)
  89. {
  90. AVCodec **p;
  91. avcodec_init();
  92. p = &first_avcodec;
  93. while (*p != NULL) p = &(*p)->next;
  94. *p = codec;
  95. codec->next = NULL;
  96. if (codec->init_static_data)
  97. codec->init_static_data(codec);
  98. }
  99. unsigned avcodec_get_edge_width(void)
  100. {
  101. return EDGE_WIDTH;
  102. }
  103. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  104. s->coded_width = width;
  105. s->coded_height= height;
  106. s->width = -((-width )>>s->lowres);
  107. s->height= -((-height)>>s->lowres);
  108. }
  109. typedef struct InternalBuffer{
  110. int last_pic_num;
  111. uint8_t *base[4];
  112. uint8_t *data[4];
  113. int linesize[4];
  114. int width, height;
  115. enum PixelFormat pix_fmt;
  116. }InternalBuffer;
  117. #define INTERNAL_BUFFER_SIZE (32+1)
  118. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  119. int w_align= 1;
  120. int h_align= 1;
  121. switch(s->pix_fmt){
  122. case PIX_FMT_YUV420P:
  123. case PIX_FMT_YUYV422:
  124. case PIX_FMT_UYVY422:
  125. case PIX_FMT_YUV422P:
  126. case PIX_FMT_YUV440P:
  127. case PIX_FMT_YUV444P:
  128. case PIX_FMT_GRAY8:
  129. case PIX_FMT_GRAY16BE:
  130. case PIX_FMT_GRAY16LE:
  131. case PIX_FMT_YUVJ420P:
  132. case PIX_FMT_YUVJ422P:
  133. case PIX_FMT_YUVJ440P:
  134. case PIX_FMT_YUVJ444P:
  135. case PIX_FMT_YUVA420P:
  136. case PIX_FMT_YUV420P9LE:
  137. case PIX_FMT_YUV420P9BE:
  138. case PIX_FMT_YUV420P10LE:
  139. case PIX_FMT_YUV420P10BE:
  140. case PIX_FMT_YUV422P9LE:
  141. case PIX_FMT_YUV422P9BE:
  142. case PIX_FMT_YUV422P10LE:
  143. case PIX_FMT_YUV422P10BE:
  144. case PIX_FMT_YUV444P9LE:
  145. case PIX_FMT_YUV444P9BE:
  146. case PIX_FMT_YUV444P10LE:
  147. case PIX_FMT_YUV444P10BE:
  148. case PIX_FMT_GBR24P:
  149. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  150. h_align= 16;
  151. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264 || s->codec_id == CODEC_ID_PRORES)
  152. h_align= 32; // interlaced is rounded up to 2 MBs
  153. break;
  154. case PIX_FMT_YUV411P:
  155. case PIX_FMT_UYYVYY411:
  156. w_align=32;
  157. h_align=8;
  158. break;
  159. case PIX_FMT_YUV410P:
  160. if(s->codec_id == CODEC_ID_SVQ1){
  161. w_align=64;
  162. h_align=64;
  163. }
  164. case PIX_FMT_RGB555:
  165. if(s->codec_id == CODEC_ID_RPZA){
  166. w_align=4;
  167. h_align=4;
  168. }
  169. case PIX_FMT_PAL8:
  170. case PIX_FMT_BGR8:
  171. case PIX_FMT_RGB8:
  172. if(s->codec_id == CODEC_ID_SMC){
  173. w_align=4;
  174. h_align=4;
  175. }
  176. break;
  177. case PIX_FMT_BGR24:
  178. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  179. w_align=4;
  180. h_align=4;
  181. }
  182. break;
  183. default:
  184. w_align= 1;
  185. h_align= 1;
  186. break;
  187. }
  188. *width = FFALIGN(*width , w_align);
  189. *height= FFALIGN(*height, h_align);
  190. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  191. *height+=2; // some of the optimized chroma MC reads one line too much
  192. // which is also done in mpeg decoders with lowres > 0
  193. linesize_align[0] =
  194. linesize_align[1] =
  195. linesize_align[2] =
  196. linesize_align[3] = STRIDE_ALIGN;
  197. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  198. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  199. //picture size unneccessarily in some cases. The solution here is not
  200. //pretty and better ideas are welcome!
  201. #if HAVE_MMX
  202. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  203. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  204. s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
  205. linesize_align[0] =
  206. linesize_align[1] =
  207. linesize_align[2] = 16;
  208. }
  209. #endif
  210. }
  211. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  212. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  213. int linesize_align[4];
  214. int align;
  215. avcodec_align_dimensions2(s, width, height, linesize_align);
  216. align = FFMAX(linesize_align[0], linesize_align[3]);
  217. linesize_align[1] <<= chroma_shift;
  218. linesize_align[2] <<= chroma_shift;
  219. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  220. *width=FFALIGN(*width, align);
  221. }
  222. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  223. {
  224. if (s->pkt) {
  225. pic->pkt_pts = s->pkt->pts;
  226. pic->pkt_pos = s->pkt->pos;
  227. } else {
  228. pic->pkt_pts = AV_NOPTS_VALUE;
  229. pic->pkt_pos = -1;
  230. }
  231. pic->reordered_opaque= s->reordered_opaque;
  232. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  233. pic->width = s->width;
  234. pic->height = s->height;
  235. pic->format = s->pix_fmt;
  236. }
  237. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  238. int i;
  239. int w= s->width;
  240. int h= s->height;
  241. InternalBuffer *buf;
  242. int *picture_number;
  243. if(pic->data[0]!=NULL) {
  244. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  245. return -1;
  246. }
  247. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  248. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  249. return -1;
  250. }
  251. if(av_image_check_size(w, h, 0, s))
  252. return -1;
  253. if(s->internal_buffer==NULL){
  254. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  255. }
  256. #if 0
  257. s->internal_buffer= av_fast_realloc(
  258. s->internal_buffer,
  259. &s->internal_buffer_size,
  260. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  261. );
  262. #endif
  263. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  264. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  265. (*picture_number)++;
  266. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  267. if(s->active_thread_type&FF_THREAD_FRAME) {
  268. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  269. return -1;
  270. }
  271. for(i=0; i<4; i++){
  272. av_freep(&buf->base[i]);
  273. buf->data[i]= NULL;
  274. }
  275. }
  276. if(buf->base[0]){
  277. pic->age= *picture_number - buf->last_pic_num;
  278. buf->last_pic_num= *picture_number;
  279. }else{
  280. int h_chroma_shift, v_chroma_shift;
  281. int size[4] = {0};
  282. int tmpsize;
  283. int unaligned;
  284. AVPicture picture;
  285. int stride_align[4];
  286. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  287. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  288. avcodec_align_dimensions2(s, &w, &h, stride_align);
  289. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  290. w+= EDGE_WIDTH*2;
  291. h+= EDGE_WIDTH*2;
  292. }
  293. do {
  294. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  295. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  296. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  297. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  298. w += w & ~(w-1);
  299. unaligned = 0;
  300. for (i=0; i<4; i++){
  301. unaligned |= picture.linesize[i] % stride_align[i];
  302. }
  303. } while (unaligned);
  304. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  305. if (tmpsize < 0)
  306. return -1;
  307. for (i=0; i<3 && picture.data[i+1]; i++)
  308. size[i] = picture.data[i+1] - picture.data[i];
  309. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  310. buf->last_pic_num= -256*256*256*64;
  311. memset(buf->base, 0, sizeof(buf->base));
  312. memset(buf->data, 0, sizeof(buf->data));
  313. for(i=0; i<4 && size[i]; i++){
  314. const int h_shift= i==0 ? 0 : h_chroma_shift;
  315. const int v_shift= i==0 ? 0 : v_chroma_shift;
  316. buf->linesize[i]= picture.linesize[i];
  317. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  318. if(buf->base[i]==NULL) return -1;
  319. memset(buf->base[i], 128, size[i]);
  320. // no edge if EDGE EMU or not planar YUV
  321. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  322. buf->data[i] = buf->base[i];
  323. else
  324. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  325. }
  326. if(size[1] && !size[2])
  327. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  328. buf->width = s->width;
  329. buf->height = s->height;
  330. buf->pix_fmt= s->pix_fmt;
  331. pic->age= 256*256*256*64;
  332. }
  333. pic->type= FF_BUFFER_TYPE_INTERNAL;
  334. for(i=0; i<4; i++){
  335. pic->base[i]= buf->base[i];
  336. pic->data[i]= buf->data[i];
  337. pic->linesize[i]= buf->linesize[i];
  338. }
  339. s->internal_buffer_count++;
  340. if (s->pkt) {
  341. pic->pkt_pts = s->pkt->pts;
  342. pic->pkt_pos = s->pkt->pos;
  343. } else {
  344. pic->pkt_pts = AV_NOPTS_VALUE;
  345. pic->pkt_pos = -1;
  346. }
  347. pic->reordered_opaque= s->reordered_opaque;
  348. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  349. pic->width = s->width;
  350. pic->height = s->height;
  351. pic->format = s->pix_fmt;
  352. if(s->debug&FF_DEBUG_BUFFERS)
  353. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  354. return 0;
  355. }
  356. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  357. int i;
  358. InternalBuffer *buf, *last;
  359. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  360. assert(s->internal_buffer_count);
  361. if(s->internal_buffer){
  362. buf = NULL; /* avoids warning */
  363. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  364. buf= &((InternalBuffer*)s->internal_buffer)[i];
  365. if(buf->data[0] == pic->data[0])
  366. break;
  367. }
  368. assert(i < s->internal_buffer_count);
  369. s->internal_buffer_count--;
  370. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  371. FFSWAP(InternalBuffer, *buf, *last);
  372. }
  373. for(i=0; i<4; i++){
  374. pic->data[i]=NULL;
  375. // pic->base[i]=NULL;
  376. }
  377. //printf("R%X\n", pic->opaque);
  378. if(s->debug&FF_DEBUG_BUFFERS)
  379. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  380. }
  381. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  382. AVFrame temp_pic;
  383. int i;
  384. /* If no picture return a new buffer */
  385. if(pic->data[0] == NULL) {
  386. /* We will copy from buffer, so must be readable */
  387. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  388. return s->get_buffer(s, pic);
  389. }
  390. /* If internal buffer type return the same buffer */
  391. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  392. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  393. else pic->pkt_pts= AV_NOPTS_VALUE;
  394. pic->reordered_opaque= s->reordered_opaque;
  395. return 0;
  396. }
  397. /*
  398. * Not internal type and reget_buffer not overridden, emulate cr buffer
  399. */
  400. temp_pic = *pic;
  401. for(i = 0; i < 4; i++)
  402. pic->data[i] = pic->base[i] = NULL;
  403. pic->opaque = NULL;
  404. /* Allocate new frame */
  405. if (s->get_buffer(s, pic))
  406. return -1;
  407. /* Copy image data from old buffer to new buffer */
  408. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  409. s->height);
  410. s->release_buffer(s, &temp_pic); // Release old frame
  411. return 0;
  412. }
  413. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  414. int i;
  415. for(i=0; i<count; i++){
  416. int r= func(c, (char*)arg + i*size);
  417. if(ret) ret[i]= r;
  418. }
  419. return 0;
  420. }
  421. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  422. int i;
  423. for(i=0; i<count; i++){
  424. int r= func(c, arg, i, 0);
  425. if(ret) ret[i]= r;
  426. }
  427. return 0;
  428. }
  429. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  430. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  431. ++fmt;
  432. return fmt[0];
  433. }
  434. void avcodec_get_frame_defaults(AVFrame *pic){
  435. memset(pic, 0, sizeof(AVFrame));
  436. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  437. pic->pkt_pos = -1;
  438. pic->key_frame= 1;
  439. pic->sample_aspect_ratio = (AVRational){0, 1};
  440. pic->format = -1; /* unknown */
  441. }
  442. AVFrame *avcodec_alloc_frame(void){
  443. AVFrame *pic= av_malloc(sizeof(AVFrame));
  444. if(pic==NULL) return NULL;
  445. avcodec_get_frame_defaults(pic);
  446. return pic;
  447. }
  448. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  449. {
  450. memset(sub, 0, sizeof(*sub));
  451. sub->pts = AV_NOPTS_VALUE;
  452. }
  453. #if FF_API_AVCODEC_OPEN
  454. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  455. {
  456. return avcodec_open2(avctx, codec, NULL);
  457. }
  458. #endif
  459. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  460. {
  461. int ret = 0;
  462. AVDictionary *tmp = NULL;
  463. if (options)
  464. av_dict_copy(&tmp, *options, 0);
  465. /* If there is a user-supplied mutex locking routine, call it. */
  466. if (ff_lockmgr_cb) {
  467. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  468. return -1;
  469. }
  470. entangled_thread_counter++;
  471. if(entangled_thread_counter != 1){
  472. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  473. ret = -1;
  474. goto end;
  475. }
  476. if(avctx->codec || !codec) {
  477. ret = AVERROR(EINVAL);
  478. goto end;
  479. }
  480. if (codec->priv_data_size > 0) {
  481. if(!avctx->priv_data){
  482. avctx->priv_data = av_mallocz(codec->priv_data_size);
  483. if (!avctx->priv_data) {
  484. ret = AVERROR(ENOMEM);
  485. goto end;
  486. }
  487. if (codec->priv_class) {
  488. *(AVClass**)avctx->priv_data= codec->priv_class;
  489. av_opt_set_defaults(avctx->priv_data);
  490. }
  491. }
  492. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  493. goto free_and_end;
  494. } else {
  495. avctx->priv_data = NULL;
  496. }
  497. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  498. goto free_and_end;
  499. if(avctx->coded_width && avctx->coded_height)
  500. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  501. else if(avctx->width && avctx->height)
  502. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  503. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  504. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  505. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  506. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  507. avcodec_set_dimensions(avctx, 0, 0);
  508. }
  509. /* if the decoder init function was already called previously,
  510. free the already allocated subtitle_header before overwriting it */
  511. if (codec->decode)
  512. av_freep(&avctx->subtitle_header);
  513. #define SANE_NB_CHANNELS 128U
  514. if (avctx->channels > SANE_NB_CHANNELS) {
  515. ret = AVERROR(EINVAL);
  516. goto free_and_end;
  517. }
  518. avctx->codec = codec;
  519. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  520. avctx->codec_id == CODEC_ID_NONE) {
  521. avctx->codec_type = codec->type;
  522. avctx->codec_id = codec->id;
  523. }
  524. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  525. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  526. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  527. ret = AVERROR(EINVAL);
  528. goto free_and_end;
  529. }
  530. avctx->frame_number = 0;
  531. #if FF_API_ER
  532. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  533. avctx->error_recognition, avctx->err_recognition);
  534. /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
  535. FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
  536. avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
  537. switch(avctx->error_recognition){
  538. case FF_ER_VERY_AGGRESSIVE:
  539. case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
  540. case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
  541. case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
  542. }
  543. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  544. avctx->error_recognition, avctx->err_recognition);
  545. #endif
  546. if (!HAVE_THREADS)
  547. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  548. if (HAVE_THREADS && !avctx->thread_opaque) {
  549. ret = ff_thread_init(avctx);
  550. if (ret < 0) {
  551. goto free_and_end;
  552. }
  553. }
  554. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  555. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  556. avctx->codec->max_lowres);
  557. ret = AVERROR(EINVAL);
  558. goto free_and_end;
  559. }
  560. if (avctx->codec->encode) {
  561. int i;
  562. if (avctx->codec->sample_fmts) {
  563. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  564. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  565. break;
  566. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  567. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  568. ret = AVERROR(EINVAL);
  569. goto free_and_end;
  570. }
  571. }
  572. if (avctx->codec->supported_samplerates) {
  573. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  574. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  575. break;
  576. if (avctx->codec->supported_samplerates[i] == 0) {
  577. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  578. ret = AVERROR(EINVAL);
  579. goto free_and_end;
  580. }
  581. }
  582. if (avctx->codec->channel_layouts) {
  583. if (!avctx->channel_layout) {
  584. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  585. } else {
  586. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  587. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  588. break;
  589. if (avctx->codec->channel_layouts[i] == 0) {
  590. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  591. ret = AVERROR(EINVAL);
  592. goto free_and_end;
  593. }
  594. }
  595. }
  596. if (avctx->channel_layout && avctx->channels) {
  597. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  598. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  599. ret = AVERROR(EINVAL);
  600. goto free_and_end;
  601. }
  602. } else if (avctx->channel_layout) {
  603. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  604. }
  605. }
  606. avctx->pts_correction_num_faulty_pts =
  607. avctx->pts_correction_num_faulty_dts = 0;
  608. avctx->pts_correction_last_pts =
  609. avctx->pts_correction_last_dts = INT64_MIN;
  610. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  611. ret = avctx->codec->init(avctx);
  612. if (ret < 0) {
  613. goto free_and_end;
  614. }
  615. }
  616. ret=0;
  617. end:
  618. entangled_thread_counter--;
  619. /* Release any user-supplied mutex. */
  620. if (ff_lockmgr_cb) {
  621. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  622. }
  623. if (options) {
  624. av_dict_free(options);
  625. *options = tmp;
  626. }
  627. return ret;
  628. free_and_end:
  629. av_dict_free(&tmp);
  630. av_freep(&avctx->priv_data);
  631. avctx->codec= NULL;
  632. goto end;
  633. }
  634. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  635. const short *samples)
  636. {
  637. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  638. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  639. return -1;
  640. }
  641. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  642. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  643. avctx->frame_number++;
  644. return ret;
  645. }else
  646. return 0;
  647. }
  648. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  649. const AVFrame *pict)
  650. {
  651. if(buf_size < FF_MIN_BUFFER_SIZE){
  652. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  653. return -1;
  654. }
  655. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  656. return -1;
  657. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  658. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  659. avctx->frame_number++;
  660. emms_c(); //needed to avoid an emms_c() call before every return;
  661. return ret;
  662. }else
  663. return 0;
  664. }
  665. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  666. const AVSubtitle *sub)
  667. {
  668. int ret;
  669. if(sub->start_display_time) {
  670. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  671. return -1;
  672. }
  673. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  674. avctx->frame_number++;
  675. return ret;
  676. }
  677. /**
  678. * Attempt to guess proper monotonic timestamps for decoded video frames
  679. * which might have incorrect times. Input timestamps may wrap around, in
  680. * which case the output will as well.
  681. *
  682. * @param pts the pts field of the decoded AVPacket, as passed through
  683. * AVFrame.pkt_pts
  684. * @param dts the dts field of the decoded AVPacket
  685. * @return one of the input values, may be AV_NOPTS_VALUE
  686. */
  687. static int64_t guess_correct_pts(AVCodecContext *ctx,
  688. int64_t reordered_pts, int64_t dts)
  689. {
  690. int64_t pts = AV_NOPTS_VALUE;
  691. if (dts != AV_NOPTS_VALUE) {
  692. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  693. ctx->pts_correction_last_dts = dts;
  694. }
  695. if (reordered_pts != AV_NOPTS_VALUE) {
  696. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  697. ctx->pts_correction_last_pts = reordered_pts;
  698. }
  699. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  700. && reordered_pts != AV_NOPTS_VALUE)
  701. pts = reordered_pts;
  702. else
  703. pts = dts;
  704. return pts;
  705. }
  706. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  707. int *got_picture_ptr,
  708. AVPacket *avpkt)
  709. {
  710. int ret;
  711. *got_picture_ptr= 0;
  712. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  713. return -1;
  714. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  715. av_packet_split_side_data(avpkt);
  716. avctx->pkt = avpkt;
  717. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  718. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  719. avpkt);
  720. else {
  721. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  722. avpkt);
  723. picture->pkt_dts= avpkt->dts;
  724. if(!avctx->has_b_frames){
  725. picture->pkt_pos= avpkt->pos;
  726. }
  727. //FIXME these should be under if(!avctx->has_b_frames)
  728. if (!picture->sample_aspect_ratio.num)
  729. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  730. if (!picture->width)
  731. picture->width = avctx->width;
  732. if (!picture->height)
  733. picture->height = avctx->height;
  734. if (picture->format == PIX_FMT_NONE)
  735. picture->format = avctx->pix_fmt;
  736. }
  737. emms_c(); //needed to avoid an emms_c() call before every return;
  738. if (*got_picture_ptr){
  739. avctx->frame_number++;
  740. picture->best_effort_timestamp = guess_correct_pts(avctx,
  741. picture->pkt_pts,
  742. picture->pkt_dts);
  743. }
  744. }else
  745. ret= 0;
  746. return ret;
  747. }
  748. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  749. int *frame_size_ptr,
  750. AVPacket *avpkt)
  751. {
  752. int ret;
  753. avctx->pkt = avpkt;
  754. if (!avpkt->data && avpkt->size) {
  755. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  756. return AVERROR(EINVAL);
  757. }
  758. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  759. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  760. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  761. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  762. return -1;
  763. }
  764. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  765. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  766. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  767. return -1;
  768. }
  769. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  770. avctx->frame_number++;
  771. }else{
  772. ret= 0;
  773. *frame_size_ptr=0;
  774. }
  775. return ret;
  776. }
  777. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  778. int *got_sub_ptr,
  779. AVPacket *avpkt)
  780. {
  781. int ret;
  782. avctx->pkt = avpkt;
  783. *got_sub_ptr = 0;
  784. avcodec_get_subtitle_defaults(sub);
  785. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  786. if (*got_sub_ptr)
  787. avctx->frame_number++;
  788. return ret;
  789. }
  790. void avsubtitle_free(AVSubtitle *sub)
  791. {
  792. int i;
  793. for (i = 0; i < sub->num_rects; i++)
  794. {
  795. av_freep(&sub->rects[i]->pict.data[0]);
  796. av_freep(&sub->rects[i]->pict.data[1]);
  797. av_freep(&sub->rects[i]->pict.data[2]);
  798. av_freep(&sub->rects[i]->pict.data[3]);
  799. av_freep(&sub->rects[i]->text);
  800. av_freep(&sub->rects[i]->ass);
  801. av_freep(&sub->rects[i]);
  802. }
  803. av_freep(&sub->rects);
  804. memset(sub, 0, sizeof(AVSubtitle));
  805. }
  806. av_cold int avcodec_close(AVCodecContext *avctx)
  807. {
  808. /* If there is a user-supplied mutex locking routine, call it. */
  809. if (ff_lockmgr_cb) {
  810. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  811. return -1;
  812. }
  813. entangled_thread_counter++;
  814. if(entangled_thread_counter != 1){
  815. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  816. entangled_thread_counter--;
  817. return -1;
  818. }
  819. if (HAVE_THREADS && avctx->thread_opaque)
  820. ff_thread_free(avctx);
  821. if (avctx->codec && avctx->codec->close)
  822. avctx->codec->close(avctx);
  823. avcodec_default_free_buffers(avctx);
  824. avctx->coded_frame = NULL;
  825. if (avctx->codec && avctx->codec->priv_class)
  826. av_opt_free(avctx->priv_data);
  827. av_opt_free(avctx);
  828. av_freep(&avctx->priv_data);
  829. if(avctx->codec && avctx->codec->encode)
  830. av_freep(&avctx->extradata);
  831. avctx->codec = NULL;
  832. avctx->active_thread_type = 0;
  833. entangled_thread_counter--;
  834. /* Release any user-supplied mutex. */
  835. if (ff_lockmgr_cb) {
  836. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  837. }
  838. return 0;
  839. }
  840. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  841. {
  842. switch(id){
  843. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  844. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  845. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  846. default : return id;
  847. }
  848. }
  849. AVCodec *avcodec_find_encoder(enum CodecID id)
  850. {
  851. AVCodec *p, *experimental=NULL;
  852. p = first_avcodec;
  853. id= remap_deprecated_codec_id(id);
  854. while (p) {
  855. if (p->encode != NULL && p->id == id) {
  856. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  857. experimental = p;
  858. } else
  859. return p;
  860. }
  861. p = p->next;
  862. }
  863. return experimental;
  864. }
  865. AVCodec *avcodec_find_encoder_by_name(const char *name)
  866. {
  867. AVCodec *p;
  868. if (!name)
  869. return NULL;
  870. p = first_avcodec;
  871. while (p) {
  872. if (p->encode != NULL && strcmp(name,p->name) == 0)
  873. return p;
  874. p = p->next;
  875. }
  876. return NULL;
  877. }
  878. AVCodec *avcodec_find_decoder(enum CodecID id)
  879. {
  880. AVCodec *p, *experimental=NULL;
  881. p = first_avcodec;
  882. id= remap_deprecated_codec_id(id);
  883. while (p) {
  884. if (p->decode != NULL && p->id == id) {
  885. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  886. experimental = p;
  887. } else
  888. return p;
  889. }
  890. p = p->next;
  891. }
  892. return experimental;
  893. }
  894. AVCodec *avcodec_find_decoder_by_name(const char *name)
  895. {
  896. AVCodec *p;
  897. if (!name)
  898. return NULL;
  899. p = first_avcodec;
  900. while (p) {
  901. if (p->decode != NULL && strcmp(name,p->name) == 0)
  902. return p;
  903. p = p->next;
  904. }
  905. return NULL;
  906. }
  907. static int get_bit_rate(AVCodecContext *ctx)
  908. {
  909. int bit_rate;
  910. int bits_per_sample;
  911. switch(ctx->codec_type) {
  912. case AVMEDIA_TYPE_VIDEO:
  913. case AVMEDIA_TYPE_DATA:
  914. case AVMEDIA_TYPE_SUBTITLE:
  915. case AVMEDIA_TYPE_ATTACHMENT:
  916. bit_rate = ctx->bit_rate;
  917. break;
  918. case AVMEDIA_TYPE_AUDIO:
  919. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  920. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  921. break;
  922. default:
  923. bit_rate = 0;
  924. break;
  925. }
  926. return bit_rate;
  927. }
  928. const char *avcodec_get_name(enum CodecID id)
  929. {
  930. AVCodec *codec;
  931. #if !CONFIG_SMALL
  932. switch (id) {
  933. #include "libavcodec/codec_names.h"
  934. }
  935. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  936. #endif
  937. codec = avcodec_find_decoder(id);
  938. if (codec)
  939. return codec->name;
  940. codec = avcodec_find_encoder(id);
  941. if (codec)
  942. return codec->name;
  943. return "unknown_codec";
  944. }
  945. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  946. {
  947. int i, len, ret = 0;
  948. for (i = 0; i < 4; i++) {
  949. len = snprintf(buf, buf_size,
  950. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  951. buf += len;
  952. buf_size = buf_size > len ? buf_size - len : 0;
  953. ret += len;
  954. codec_tag>>=8;
  955. }
  956. return ret;
  957. }
  958. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  959. {
  960. const char *codec_type;
  961. const char *codec_name;
  962. const char *profile = NULL;
  963. AVCodec *p;
  964. int bitrate;
  965. AVRational display_aspect_ratio;
  966. if (!buf || buf_size <= 0)
  967. return;
  968. codec_type = av_get_media_type_string(enc->codec_type);
  969. codec_name = avcodec_get_name(enc->codec_id);
  970. if (enc->profile != FF_PROFILE_UNKNOWN) {
  971. p = encode ? avcodec_find_encoder(enc->codec_id) :
  972. avcodec_find_decoder(enc->codec_id);
  973. if (p)
  974. profile = av_get_profile_name(p, enc->profile);
  975. }
  976. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  977. codec_name, enc->mb_decision ? " (hq)" : "");
  978. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  979. if (profile)
  980. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  981. if (enc->codec_tag) {
  982. char tag_buf[32];
  983. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  984. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  985. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  986. }
  987. switch(enc->codec_type) {
  988. case AVMEDIA_TYPE_VIDEO:
  989. if (enc->pix_fmt != PIX_FMT_NONE) {
  990. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  991. ", %s",
  992. av_get_pix_fmt_name(enc->pix_fmt));
  993. }
  994. if (enc->width) {
  995. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  996. ", %dx%d",
  997. enc->width, enc->height);
  998. if (enc->sample_aspect_ratio.num) {
  999. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1000. enc->width*enc->sample_aspect_ratio.num,
  1001. enc->height*enc->sample_aspect_ratio.den,
  1002. 1024*1024);
  1003. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1004. " [SAR %d:%d DAR %d:%d]",
  1005. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1006. display_aspect_ratio.num, display_aspect_ratio.den);
  1007. }
  1008. if(av_log_get_level() >= AV_LOG_DEBUG){
  1009. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1010. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1011. ", %d/%d",
  1012. enc->time_base.num/g, enc->time_base.den/g);
  1013. }
  1014. }
  1015. if (encode) {
  1016. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1017. ", q=%d-%d", enc->qmin, enc->qmax);
  1018. }
  1019. break;
  1020. case AVMEDIA_TYPE_AUDIO:
  1021. if (enc->sample_rate) {
  1022. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1023. ", %d Hz", enc->sample_rate);
  1024. }
  1025. av_strlcat(buf, ", ", buf_size);
  1026. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1027. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1028. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1029. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1030. }
  1031. break;
  1032. default:
  1033. return;
  1034. }
  1035. if (encode) {
  1036. if (enc->flags & CODEC_FLAG_PASS1)
  1037. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1038. ", pass 1");
  1039. if (enc->flags & CODEC_FLAG_PASS2)
  1040. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1041. ", pass 2");
  1042. }
  1043. bitrate = get_bit_rate(enc);
  1044. if (bitrate != 0) {
  1045. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1046. ", %d kb/s", bitrate / 1000);
  1047. }
  1048. }
  1049. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1050. {
  1051. const AVProfile *p;
  1052. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1053. return NULL;
  1054. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1055. if (p->profile == profile)
  1056. return p->name;
  1057. return NULL;
  1058. }
  1059. unsigned avcodec_version( void )
  1060. {
  1061. return LIBAVCODEC_VERSION_INT;
  1062. }
  1063. const char *avcodec_configuration(void)
  1064. {
  1065. return FFMPEG_CONFIGURATION;
  1066. }
  1067. const char *avcodec_license(void)
  1068. {
  1069. #define LICENSE_PREFIX "libavcodec license: "
  1070. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1071. }
  1072. void avcodec_flush_buffers(AVCodecContext *avctx)
  1073. {
  1074. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1075. ff_thread_flush(avctx);
  1076. else if(avctx->codec->flush)
  1077. avctx->codec->flush(avctx);
  1078. }
  1079. void avcodec_default_free_buffers(AVCodecContext *s){
  1080. int i, j;
  1081. if(s->internal_buffer==NULL) return;
  1082. if (s->internal_buffer_count)
  1083. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1084. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1085. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1086. for(j=0; j<4; j++){
  1087. av_freep(&buf->base[j]);
  1088. buf->data[j]= NULL;
  1089. }
  1090. }
  1091. av_freep(&s->internal_buffer);
  1092. s->internal_buffer_count=0;
  1093. }
  1094. #if FF_API_OLD_FF_PICT_TYPES
  1095. char av_get_pict_type_char(int pict_type){
  1096. return av_get_picture_type_char(pict_type);
  1097. }
  1098. #endif
  1099. int av_get_bits_per_sample(enum CodecID codec_id){
  1100. switch(codec_id){
  1101. case CODEC_ID_ADPCM_SBPRO_2:
  1102. return 2;
  1103. case CODEC_ID_ADPCM_SBPRO_3:
  1104. return 3;
  1105. case CODEC_ID_ADPCM_SBPRO_4:
  1106. case CODEC_ID_ADPCM_CT:
  1107. case CODEC_ID_ADPCM_IMA_WAV:
  1108. case CODEC_ID_ADPCM_IMA_QT:
  1109. case CODEC_ID_ADPCM_SWF:
  1110. case CODEC_ID_ADPCM_MS:
  1111. case CODEC_ID_ADPCM_YAMAHA:
  1112. return 4;
  1113. case CODEC_ID_ADPCM_G722:
  1114. case CODEC_ID_PCM_ALAW:
  1115. case CODEC_ID_PCM_MULAW:
  1116. case CODEC_ID_PCM_S8:
  1117. case CODEC_ID_PCM_U8:
  1118. case CODEC_ID_PCM_ZORK:
  1119. return 8;
  1120. case CODEC_ID_PCM_S16BE:
  1121. case CODEC_ID_PCM_S16LE:
  1122. case CODEC_ID_PCM_S16LE_PLANAR:
  1123. case CODEC_ID_PCM_U16BE:
  1124. case CODEC_ID_PCM_U16LE:
  1125. return 16;
  1126. case CODEC_ID_PCM_S24DAUD:
  1127. case CODEC_ID_PCM_S24BE:
  1128. case CODEC_ID_PCM_S24LE:
  1129. case CODEC_ID_PCM_U24BE:
  1130. case CODEC_ID_PCM_U24LE:
  1131. return 24;
  1132. case CODEC_ID_PCM_S32BE:
  1133. case CODEC_ID_PCM_S32LE:
  1134. case CODEC_ID_PCM_U32BE:
  1135. case CODEC_ID_PCM_U32LE:
  1136. case CODEC_ID_PCM_F32BE:
  1137. case CODEC_ID_PCM_F32LE:
  1138. return 32;
  1139. case CODEC_ID_PCM_F64BE:
  1140. case CODEC_ID_PCM_F64LE:
  1141. return 64;
  1142. default:
  1143. return 0;
  1144. }
  1145. }
  1146. #if FF_API_OLD_SAMPLE_FMT
  1147. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1148. return av_get_bytes_per_sample(sample_fmt) << 3;
  1149. }
  1150. #endif
  1151. #if !HAVE_THREADS
  1152. int ff_thread_init(AVCodecContext *s){
  1153. return -1;
  1154. }
  1155. #endif
  1156. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1157. {
  1158. unsigned int n = 0;
  1159. while(v >= 0xff) {
  1160. *s++ = 0xff;
  1161. v -= 0xff;
  1162. n++;
  1163. }
  1164. *s = v;
  1165. n++;
  1166. return n;
  1167. }
  1168. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1169. int i;
  1170. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1171. return i;
  1172. }
  1173. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1174. {
  1175. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1176. "version to the newest one from Git. If the problem still "
  1177. "occurs, it means that your file has a feature which has not "
  1178. "been implemented.\n", feature);
  1179. if(want_sample)
  1180. av_log_ask_for_sample(avc, NULL);
  1181. }
  1182. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1183. {
  1184. va_list argument_list;
  1185. va_start(argument_list, msg);
  1186. if (msg)
  1187. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1188. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1189. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1190. "and contact the ffmpeg-devel mailing list.\n");
  1191. va_end(argument_list);
  1192. }
  1193. static AVHWAccel *first_hwaccel = NULL;
  1194. void av_register_hwaccel(AVHWAccel *hwaccel)
  1195. {
  1196. AVHWAccel **p = &first_hwaccel;
  1197. while (*p)
  1198. p = &(*p)->next;
  1199. *p = hwaccel;
  1200. hwaccel->next = NULL;
  1201. }
  1202. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1203. {
  1204. return hwaccel ? hwaccel->next : first_hwaccel;
  1205. }
  1206. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1207. {
  1208. AVHWAccel *hwaccel=NULL;
  1209. while((hwaccel= av_hwaccel_next(hwaccel))){
  1210. if ( hwaccel->id == codec_id
  1211. && hwaccel->pix_fmt == pix_fmt)
  1212. return hwaccel;
  1213. }
  1214. return NULL;
  1215. }
  1216. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1217. {
  1218. if (ff_lockmgr_cb) {
  1219. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1220. return -1;
  1221. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1222. return -1;
  1223. }
  1224. ff_lockmgr_cb = cb;
  1225. if (ff_lockmgr_cb) {
  1226. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1227. return -1;
  1228. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1229. return -1;
  1230. }
  1231. return 0;
  1232. }
  1233. int avpriv_lock_avformat(void)
  1234. {
  1235. if (ff_lockmgr_cb) {
  1236. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1237. return -1;
  1238. }
  1239. return 0;
  1240. }
  1241. int avpriv_unlock_avformat(void)
  1242. {
  1243. if (ff_lockmgr_cb) {
  1244. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1245. return -1;
  1246. }
  1247. return 0;
  1248. }
  1249. unsigned int avpriv_toupper4(unsigned int x)
  1250. {
  1251. return toupper( x &0xFF)
  1252. + (toupper((x>>8 )&0xFF)<<8 )
  1253. + (toupper((x>>16)&0xFF)<<16)
  1254. + (toupper((x>>24)&0xFF)<<24);
  1255. }
  1256. #if !HAVE_THREADS
  1257. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1258. {
  1259. f->owner = avctx;
  1260. ff_init_buffer_info(avctx, f);
  1261. return avctx->get_buffer(avctx, f);
  1262. }
  1263. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1264. {
  1265. f->owner->release_buffer(f->owner, f);
  1266. }
  1267. void ff_thread_finish_setup(AVCodecContext *avctx)
  1268. {
  1269. }
  1270. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1271. {
  1272. }
  1273. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1274. {
  1275. }
  1276. #endif
  1277. #if FF_API_THREAD_INIT
  1278. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1279. {
  1280. s->thread_count = thread_count;
  1281. return ff_thread_init(s);
  1282. }
  1283. #endif
  1284. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1285. {
  1286. AVCodec *c= avcodec_find_decoder(codec_id);
  1287. if(!c)
  1288. c= avcodec_find_encoder(codec_id);
  1289. if(c)
  1290. return c->type;
  1291. if (codec_id <= CODEC_ID_NONE)
  1292. return AVMEDIA_TYPE_UNKNOWN;
  1293. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1294. return AVMEDIA_TYPE_VIDEO;
  1295. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1296. return AVMEDIA_TYPE_AUDIO;
  1297. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1298. return AVMEDIA_TYPE_SUBTITLE;
  1299. return AVMEDIA_TYPE_UNKNOWN;
  1300. }