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.

1218 lines
35KB

  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/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "libavutil/opt.h"
  36. #include "imgconvert.h"
  37. #include "thread.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. min_size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, min_size);
  53. 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
  54. min_size= 0;
  55. *size= min_size;
  56. return ptr;
  57. }
  58. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  59. {
  60. void **p = ptr;
  61. if (min_size < *size)
  62. return;
  63. min_size= FFMAX(17*min_size/16 + 32, min_size);
  64. av_free(*p);
  65. *p = av_malloc(min_size);
  66. if (!*p) min_size = 0;
  67. *size= min_size;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. unsigned avcodec_get_edge_width(void)
  85. {
  86. return EDGE_WIDTH;
  87. }
  88. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  89. s->coded_width = width;
  90. s->coded_height= height;
  91. s->width = -((-width )>>s->lowres);
  92. s->height= -((-height)>>s->lowres);
  93. }
  94. typedef struct InternalBuffer{
  95. int last_pic_num;
  96. uint8_t *base[4];
  97. uint8_t *data[4];
  98. int linesize[4];
  99. int width, height;
  100. enum PixelFormat pix_fmt;
  101. }InternalBuffer;
  102. #define INTERNAL_BUFFER_SIZE 32
  103. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  104. int w_align= 1;
  105. int h_align= 1;
  106. switch(s->pix_fmt){
  107. case PIX_FMT_YUV420P:
  108. case PIX_FMT_YUYV422:
  109. case PIX_FMT_UYVY422:
  110. case PIX_FMT_YUV422P:
  111. case PIX_FMT_YUV440P:
  112. case PIX_FMT_YUV444P:
  113. case PIX_FMT_GRAY8:
  114. case PIX_FMT_GRAY16BE:
  115. case PIX_FMT_GRAY16LE:
  116. case PIX_FMT_YUVJ420P:
  117. case PIX_FMT_YUVJ422P:
  118. case PIX_FMT_YUVJ440P:
  119. case PIX_FMT_YUVJ444P:
  120. case PIX_FMT_YUVA420P:
  121. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  122. h_align= 16;
  123. 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)
  124. h_align= 32; // interlaced is rounded up to 2 MBs
  125. break;
  126. case PIX_FMT_YUV411P:
  127. case PIX_FMT_UYYVYY411:
  128. w_align=32;
  129. h_align=8;
  130. break;
  131. case PIX_FMT_YUV410P:
  132. if(s->codec_id == CODEC_ID_SVQ1){
  133. w_align=64;
  134. h_align=64;
  135. }
  136. case PIX_FMT_RGB555:
  137. if(s->codec_id == CODEC_ID_RPZA){
  138. w_align=4;
  139. h_align=4;
  140. }
  141. case PIX_FMT_PAL8:
  142. case PIX_FMT_BGR8:
  143. case PIX_FMT_RGB8:
  144. if(s->codec_id == CODEC_ID_SMC){
  145. w_align=4;
  146. h_align=4;
  147. }
  148. break;
  149. case PIX_FMT_BGR24:
  150. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  151. w_align=4;
  152. h_align=4;
  153. }
  154. break;
  155. default:
  156. w_align= 1;
  157. h_align= 1;
  158. break;
  159. }
  160. *width = FFALIGN(*width , w_align);
  161. *height= FFALIGN(*height, h_align);
  162. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  163. *height+=2; // some of the optimized chroma MC reads one line too much
  164. // which is also done in mpeg decoders with lowres > 0
  165. linesize_align[0] =
  166. linesize_align[1] =
  167. linesize_align[2] =
  168. linesize_align[3] = STRIDE_ALIGN;
  169. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  170. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  171. //picture size unneccessarily in some cases. The solution here is not
  172. //pretty and better ideas are welcome!
  173. #if HAVE_MMX
  174. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  175. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  176. s->codec_id == CODEC_ID_VP6A) {
  177. linesize_align[0] =
  178. linesize_align[1] =
  179. linesize_align[2] = 16;
  180. }
  181. #endif
  182. }
  183. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  184. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  185. int linesize_align[4];
  186. int align;
  187. avcodec_align_dimensions2(s, width, height, linesize_align);
  188. align = FFMAX(linesize_align[0], linesize_align[3]);
  189. linesize_align[1] <<= chroma_shift;
  190. linesize_align[2] <<= chroma_shift;
  191. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  192. *width=FFALIGN(*width, align);
  193. }
  194. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  195. int i;
  196. int w= s->width;
  197. int h= s->height;
  198. InternalBuffer *buf;
  199. int *picture_number;
  200. if(pic->data[0]!=NULL) {
  201. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  202. return -1;
  203. }
  204. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  205. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  206. return -1;
  207. }
  208. if(av_image_check_size(w, h, 0, s))
  209. return -1;
  210. if(s->internal_buffer==NULL){
  211. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  212. }
  213. #if 0
  214. s->internal_buffer= av_fast_realloc(
  215. s->internal_buffer,
  216. &s->internal_buffer_size,
  217. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  218. );
  219. #endif
  220. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  221. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  222. (*picture_number)++;
  223. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  224. if(s->active_thread_type&FF_THREAD_FRAME) {
  225. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  226. return -1;
  227. }
  228. for(i=0; i<4; i++){
  229. av_freep(&buf->base[i]);
  230. buf->data[i]= NULL;
  231. }
  232. }
  233. if(buf->base[0]){
  234. pic->age= *picture_number - buf->last_pic_num;
  235. buf->last_pic_num= *picture_number;
  236. }else{
  237. int h_chroma_shift, v_chroma_shift;
  238. int size[4] = {0};
  239. int tmpsize;
  240. int unaligned;
  241. AVPicture picture;
  242. int stride_align[4];
  243. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  244. avcodec_align_dimensions2(s, &w, &h, stride_align);
  245. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  246. w+= EDGE_WIDTH*2;
  247. h+= EDGE_WIDTH*2;
  248. }
  249. do {
  250. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  251. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  252. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  253. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  254. w += w & ~(w-1);
  255. unaligned = 0;
  256. for (i=0; i<4; i++){
  257. unaligned |= picture.linesize[i] % stride_align[i];
  258. }
  259. } while (unaligned);
  260. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  261. if (tmpsize < 0)
  262. return -1;
  263. for (i=0; i<3 && picture.data[i+1]; i++)
  264. size[i] = picture.data[i+1] - picture.data[i];
  265. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  266. buf->last_pic_num= -256*256*256*64;
  267. memset(buf->base, 0, sizeof(buf->base));
  268. memset(buf->data, 0, sizeof(buf->data));
  269. for(i=0; i<4 && size[i]; i++){
  270. const int h_shift= i==0 ? 0 : h_chroma_shift;
  271. const int v_shift= i==0 ? 0 : v_chroma_shift;
  272. buf->linesize[i]= picture.linesize[i];
  273. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  274. if(buf->base[i]==NULL) return -1;
  275. memset(buf->base[i], 128, size[i]);
  276. // no edge if EDGE EMU or not planar YUV
  277. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  278. buf->data[i] = buf->base[i];
  279. else
  280. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  281. }
  282. if(size[1] && !size[2])
  283. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  284. buf->width = s->width;
  285. buf->height = s->height;
  286. buf->pix_fmt= s->pix_fmt;
  287. pic->age= 256*256*256*64;
  288. }
  289. pic->type= FF_BUFFER_TYPE_INTERNAL;
  290. for(i=0; i<4; i++){
  291. pic->base[i]= buf->base[i];
  292. pic->data[i]= buf->data[i];
  293. pic->linesize[i]= buf->linesize[i];
  294. }
  295. s->internal_buffer_count++;
  296. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  297. else pic->pkt_pts= AV_NOPTS_VALUE;
  298. pic->reordered_opaque= s->reordered_opaque;
  299. if(s->debug&FF_DEBUG_BUFFERS)
  300. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  301. return 0;
  302. }
  303. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  304. int i;
  305. InternalBuffer *buf, *last;
  306. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  307. assert(s->internal_buffer_count);
  308. buf = NULL; /* avoids warning */
  309. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  310. buf= &((InternalBuffer*)s->internal_buffer)[i];
  311. if(buf->data[0] == pic->data[0])
  312. break;
  313. }
  314. assert(i < s->internal_buffer_count);
  315. s->internal_buffer_count--;
  316. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  317. FFSWAP(InternalBuffer, *buf, *last);
  318. for(i=0; i<4; i++){
  319. pic->data[i]=NULL;
  320. // pic->base[i]=NULL;
  321. }
  322. //printf("R%X\n", pic->opaque);
  323. if(s->debug&FF_DEBUG_BUFFERS)
  324. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  325. }
  326. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  327. AVFrame temp_pic;
  328. int i;
  329. /* If no picture return a new buffer */
  330. if(pic->data[0] == NULL) {
  331. /* We will copy from buffer, so must be readable */
  332. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  333. return s->get_buffer(s, pic);
  334. }
  335. /* If internal buffer type return the same buffer */
  336. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  337. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  338. else pic->pkt_pts= AV_NOPTS_VALUE;
  339. pic->reordered_opaque= s->reordered_opaque;
  340. return 0;
  341. }
  342. /*
  343. * Not internal type and reget_buffer not overridden, emulate cr buffer
  344. */
  345. temp_pic = *pic;
  346. for(i = 0; i < 4; i++)
  347. pic->data[i] = pic->base[i] = NULL;
  348. pic->opaque = NULL;
  349. /* Allocate new frame */
  350. if (s->get_buffer(s, pic))
  351. return -1;
  352. /* Copy image data from old buffer to new buffer */
  353. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  354. s->height);
  355. s->release_buffer(s, &temp_pic); // Release old frame
  356. return 0;
  357. }
  358. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  359. int i;
  360. for(i=0; i<count; i++){
  361. int r= func(c, (char*)arg + i*size);
  362. if(ret) ret[i]= r;
  363. }
  364. return 0;
  365. }
  366. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  367. int i;
  368. for(i=0; i<count; i++){
  369. int r= func(c, arg, i, 0);
  370. if(ret) ret[i]= r;
  371. }
  372. return 0;
  373. }
  374. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  375. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  376. ++fmt;
  377. return fmt[0];
  378. }
  379. void avcodec_get_frame_defaults(AVFrame *pic){
  380. memset(pic, 0, sizeof(AVFrame));
  381. pic->pts= AV_NOPTS_VALUE;
  382. pic->key_frame= 1;
  383. }
  384. AVFrame *avcodec_alloc_frame(void){
  385. AVFrame *pic= av_malloc(sizeof(AVFrame));
  386. if(pic==NULL) return NULL;
  387. avcodec_get_frame_defaults(pic);
  388. return pic;
  389. }
  390. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  391. {
  392. int ret= -1;
  393. /* If there is a user-supplied mutex locking routine, call it. */
  394. if (ff_lockmgr_cb) {
  395. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  396. return -1;
  397. }
  398. entangled_thread_counter++;
  399. if(entangled_thread_counter != 1){
  400. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  401. goto end;
  402. }
  403. if(avctx->codec || !codec)
  404. goto end;
  405. if (codec->priv_data_size > 0) {
  406. if(!avctx->priv_data){
  407. avctx->priv_data = av_mallocz(codec->priv_data_size);
  408. if (!avctx->priv_data) {
  409. ret = AVERROR(ENOMEM);
  410. goto end;
  411. }
  412. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  413. *(AVClass**)avctx->priv_data= codec->priv_class;
  414. av_opt_set_defaults(avctx->priv_data);
  415. }
  416. }
  417. } else {
  418. avctx->priv_data = NULL;
  419. }
  420. if(avctx->coded_width && avctx->coded_height)
  421. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  422. else if(avctx->width && avctx->height)
  423. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  424. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  425. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  426. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  427. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  428. avcodec_set_dimensions(avctx, 0, 0);
  429. }
  430. /* if the decoder init function was already called previously,
  431. free the already allocated subtitle_header before overwriting it */
  432. if (codec->decode)
  433. av_freep(&avctx->subtitle_header);
  434. #define SANE_NB_CHANNELS 128U
  435. if (avctx->channels > SANE_NB_CHANNELS) {
  436. ret = AVERROR(EINVAL);
  437. goto free_and_end;
  438. }
  439. avctx->codec = codec;
  440. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  441. avctx->codec_id == CODEC_ID_NONE) {
  442. avctx->codec_type = codec->type;
  443. avctx->codec_id = codec->id;
  444. }
  445. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  446. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  447. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  448. goto free_and_end;
  449. }
  450. avctx->frame_number = 0;
  451. if (HAVE_THREADS && !avctx->thread_opaque) {
  452. ret = ff_thread_init(avctx);
  453. if (ret < 0) {
  454. goto free_and_end;
  455. }
  456. }
  457. if (avctx->codec->max_lowres < avctx->lowres) {
  458. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  459. avctx->codec->max_lowres);
  460. goto free_and_end;
  461. }
  462. if (avctx->codec->sample_fmts && avctx->codec->encode) {
  463. int i;
  464. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  465. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  466. break;
  467. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  468. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  469. goto free_and_end;
  470. }
  471. }
  472. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  473. ret = avctx->codec->init(avctx);
  474. if (ret < 0) {
  475. goto free_and_end;
  476. }
  477. }
  478. ret=0;
  479. end:
  480. entangled_thread_counter--;
  481. /* Release any user-supplied mutex. */
  482. if (ff_lockmgr_cb) {
  483. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  484. }
  485. return ret;
  486. free_and_end:
  487. av_freep(&avctx->priv_data);
  488. avctx->codec= NULL;
  489. goto end;
  490. }
  491. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  492. const short *samples)
  493. {
  494. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  495. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  496. return -1;
  497. }
  498. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  499. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  500. avctx->frame_number++;
  501. return ret;
  502. }else
  503. return 0;
  504. }
  505. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  506. const AVFrame *pict)
  507. {
  508. if(buf_size < FF_MIN_BUFFER_SIZE){
  509. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  510. return -1;
  511. }
  512. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  513. return -1;
  514. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  515. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  516. avctx->frame_number++;
  517. emms_c(); //needed to avoid an emms_c() call before every return;
  518. return ret;
  519. }else
  520. return 0;
  521. }
  522. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  523. const AVSubtitle *sub)
  524. {
  525. int ret;
  526. if(sub->start_display_time) {
  527. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  528. return -1;
  529. }
  530. if(sub->num_rects == 0 || !sub->rects)
  531. return -1;
  532. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  533. avctx->frame_number++;
  534. return ret;
  535. }
  536. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  537. int *got_picture_ptr,
  538. AVPacket *avpkt)
  539. {
  540. int ret;
  541. *got_picture_ptr= 0;
  542. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  543. return -1;
  544. avctx->pkt = avpkt;
  545. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  546. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  547. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  548. avpkt);
  549. else {
  550. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  551. avpkt);
  552. picture->pkt_dts= avpkt->dts;
  553. }
  554. emms_c(); //needed to avoid an emms_c() call before every return;
  555. if (*got_picture_ptr)
  556. avctx->frame_number++;
  557. }else
  558. ret= 0;
  559. return ret;
  560. }
  561. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  562. int *frame_size_ptr,
  563. AVPacket *avpkt)
  564. {
  565. int ret;
  566. avctx->pkt = avpkt;
  567. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  568. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  569. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  570. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  571. return -1;
  572. }
  573. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  574. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  575. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  576. return -1;
  577. }
  578. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  579. avctx->frame_number++;
  580. }else{
  581. ret= 0;
  582. *frame_size_ptr=0;
  583. }
  584. return ret;
  585. }
  586. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  587. int *got_sub_ptr,
  588. AVPacket *avpkt)
  589. {
  590. int ret;
  591. avctx->pkt = avpkt;
  592. *got_sub_ptr = 0;
  593. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  594. if (*got_sub_ptr)
  595. avctx->frame_number++;
  596. return ret;
  597. }
  598. void avsubtitle_free(AVSubtitle *sub)
  599. {
  600. int i;
  601. for (i = 0; i < sub->num_rects; i++)
  602. {
  603. av_freep(&sub->rects[i]->pict.data[0]);
  604. av_freep(&sub->rects[i]->pict.data[1]);
  605. av_freep(&sub->rects[i]->pict.data[2]);
  606. av_freep(&sub->rects[i]->pict.data[3]);
  607. av_freep(&sub->rects[i]->text);
  608. av_freep(&sub->rects[i]->ass);
  609. av_freep(&sub->rects[i]);
  610. }
  611. av_freep(&sub->rects);
  612. memset(sub, 0, sizeof(AVSubtitle));
  613. }
  614. av_cold int avcodec_close(AVCodecContext *avctx)
  615. {
  616. /* If there is a user-supplied mutex locking routine, call it. */
  617. if (ff_lockmgr_cb) {
  618. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  619. return -1;
  620. }
  621. entangled_thread_counter++;
  622. if(entangled_thread_counter != 1){
  623. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  624. entangled_thread_counter--;
  625. return -1;
  626. }
  627. if (HAVE_THREADS && avctx->thread_opaque)
  628. ff_thread_free(avctx);
  629. if (avctx->codec && avctx->codec->close)
  630. avctx->codec->close(avctx);
  631. avcodec_default_free_buffers(avctx);
  632. avctx->coded_frame = NULL;
  633. av_freep(&avctx->priv_data);
  634. if(avctx->codec && avctx->codec->encode)
  635. av_freep(&avctx->extradata);
  636. avctx->codec = NULL;
  637. avctx->active_thread_type = 0;
  638. entangled_thread_counter--;
  639. /* Release any user-supplied mutex. */
  640. if (ff_lockmgr_cb) {
  641. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  642. }
  643. return 0;
  644. }
  645. AVCodec *avcodec_find_encoder(enum CodecID id)
  646. {
  647. AVCodec *p, *experimental=NULL;
  648. p = first_avcodec;
  649. while (p) {
  650. if (p->encode != NULL && p->id == id) {
  651. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  652. experimental = p;
  653. } else
  654. return p;
  655. }
  656. p = p->next;
  657. }
  658. return experimental;
  659. }
  660. AVCodec *avcodec_find_encoder_by_name(const char *name)
  661. {
  662. AVCodec *p;
  663. if (!name)
  664. return NULL;
  665. p = first_avcodec;
  666. while (p) {
  667. if (p->encode != NULL && strcmp(name,p->name) == 0)
  668. return p;
  669. p = p->next;
  670. }
  671. return NULL;
  672. }
  673. AVCodec *avcodec_find_decoder(enum CodecID id)
  674. {
  675. AVCodec *p;
  676. p = first_avcodec;
  677. while (p) {
  678. if (p->decode != NULL && p->id == id)
  679. return p;
  680. p = p->next;
  681. }
  682. return NULL;
  683. }
  684. AVCodec *avcodec_find_decoder_by_name(const char *name)
  685. {
  686. AVCodec *p;
  687. if (!name)
  688. return NULL;
  689. p = first_avcodec;
  690. while (p) {
  691. if (p->decode != NULL && strcmp(name,p->name) == 0)
  692. return p;
  693. p = p->next;
  694. }
  695. return NULL;
  696. }
  697. static int get_bit_rate(AVCodecContext *ctx)
  698. {
  699. int bit_rate;
  700. int bits_per_sample;
  701. switch(ctx->codec_type) {
  702. case AVMEDIA_TYPE_VIDEO:
  703. case AVMEDIA_TYPE_DATA:
  704. case AVMEDIA_TYPE_SUBTITLE:
  705. case AVMEDIA_TYPE_ATTACHMENT:
  706. bit_rate = ctx->bit_rate;
  707. break;
  708. case AVMEDIA_TYPE_AUDIO:
  709. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  710. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  711. break;
  712. default:
  713. bit_rate = 0;
  714. break;
  715. }
  716. return bit_rate;
  717. }
  718. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  719. {
  720. int i, len, ret = 0;
  721. for (i = 0; i < 4; i++) {
  722. len = snprintf(buf, buf_size,
  723. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  724. buf += len;
  725. buf_size = buf_size > len ? buf_size - len : 0;
  726. ret += len;
  727. codec_tag>>=8;
  728. }
  729. return ret;
  730. }
  731. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  732. {
  733. const char *codec_name;
  734. const char *profile = NULL;
  735. AVCodec *p;
  736. char buf1[32];
  737. int bitrate;
  738. AVRational display_aspect_ratio;
  739. if (encode)
  740. p = avcodec_find_encoder(enc->codec_id);
  741. else
  742. p = avcodec_find_decoder(enc->codec_id);
  743. if (p) {
  744. codec_name = p->name;
  745. profile = av_get_profile_name(p, enc->profile);
  746. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  747. /* fake mpeg2 transport stream codec (currently not
  748. registered) */
  749. codec_name = "mpeg2ts";
  750. } else if (enc->codec_name[0] != '\0') {
  751. codec_name = enc->codec_name;
  752. } else {
  753. /* output avi tags */
  754. char tag_buf[32];
  755. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  756. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  757. codec_name = buf1;
  758. }
  759. switch(enc->codec_type) {
  760. case AVMEDIA_TYPE_VIDEO:
  761. snprintf(buf, buf_size,
  762. "Video: %s%s",
  763. codec_name, enc->mb_decision ? " (hq)" : "");
  764. if (profile)
  765. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  766. " (%s)", profile);
  767. if (enc->pix_fmt != PIX_FMT_NONE) {
  768. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  769. ", %s",
  770. avcodec_get_pix_fmt_name(enc->pix_fmt));
  771. }
  772. if (enc->width) {
  773. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  774. ", %dx%d",
  775. enc->width, enc->height);
  776. if (enc->sample_aspect_ratio.num) {
  777. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  778. enc->width*enc->sample_aspect_ratio.num,
  779. enc->height*enc->sample_aspect_ratio.den,
  780. 1024*1024);
  781. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  782. " [PAR %d:%d DAR %d:%d]",
  783. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  784. display_aspect_ratio.num, display_aspect_ratio.den);
  785. }
  786. if(av_log_get_level() >= AV_LOG_DEBUG){
  787. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  788. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  789. ", %d/%d",
  790. enc->time_base.num/g, enc->time_base.den/g);
  791. }
  792. }
  793. if (encode) {
  794. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  795. ", q=%d-%d", enc->qmin, enc->qmax);
  796. }
  797. break;
  798. case AVMEDIA_TYPE_AUDIO:
  799. snprintf(buf, buf_size,
  800. "Audio: %s",
  801. codec_name);
  802. if (profile)
  803. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  804. " (%s)", profile);
  805. if (enc->sample_rate) {
  806. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  807. ", %d Hz", enc->sample_rate);
  808. }
  809. av_strlcat(buf, ", ", buf_size);
  810. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  811. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  812. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  813. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  814. }
  815. break;
  816. case AVMEDIA_TYPE_DATA:
  817. snprintf(buf, buf_size, "Data: %s", codec_name);
  818. break;
  819. case AVMEDIA_TYPE_SUBTITLE:
  820. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  821. break;
  822. case AVMEDIA_TYPE_ATTACHMENT:
  823. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  824. break;
  825. default:
  826. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  827. return;
  828. }
  829. if (encode) {
  830. if (enc->flags & CODEC_FLAG_PASS1)
  831. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  832. ", pass 1");
  833. if (enc->flags & CODEC_FLAG_PASS2)
  834. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  835. ", pass 2");
  836. }
  837. bitrate = get_bit_rate(enc);
  838. if (bitrate != 0) {
  839. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  840. ", %d kb/s", bitrate / 1000);
  841. }
  842. }
  843. const char *av_get_profile_name(const AVCodec *codec, int profile)
  844. {
  845. const AVProfile *p;
  846. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  847. return NULL;
  848. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  849. if (p->profile == profile)
  850. return p->name;
  851. return NULL;
  852. }
  853. unsigned avcodec_version( void )
  854. {
  855. return LIBAVCODEC_VERSION_INT;
  856. }
  857. const char *avcodec_configuration(void)
  858. {
  859. return LIBAV_CONFIGURATION;
  860. }
  861. const char *avcodec_license(void)
  862. {
  863. #define LICENSE_PREFIX "libavcodec license: "
  864. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  865. }
  866. void avcodec_init(void)
  867. {
  868. static int initialized = 0;
  869. if (initialized != 0)
  870. return;
  871. initialized = 1;
  872. dsputil_static_init();
  873. }
  874. void avcodec_flush_buffers(AVCodecContext *avctx)
  875. {
  876. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  877. ff_thread_flush(avctx);
  878. if(avctx->codec->flush)
  879. avctx->codec->flush(avctx);
  880. }
  881. void avcodec_default_free_buffers(AVCodecContext *s){
  882. int i, j;
  883. if(s->internal_buffer==NULL) return;
  884. if (s->internal_buffer_count)
  885. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  886. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  887. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  888. for(j=0; j<4; j++){
  889. av_freep(&buf->base[j]);
  890. buf->data[j]= NULL;
  891. }
  892. }
  893. av_freep(&s->internal_buffer);
  894. s->internal_buffer_count=0;
  895. }
  896. char av_get_pict_type_char(int pict_type){
  897. switch(pict_type){
  898. case FF_I_TYPE: return 'I';
  899. case FF_P_TYPE: return 'P';
  900. case FF_B_TYPE: return 'B';
  901. case FF_S_TYPE: return 'S';
  902. case FF_SI_TYPE:return 'i';
  903. case FF_SP_TYPE:return 'p';
  904. case FF_BI_TYPE:return 'b';
  905. default: return '?';
  906. }
  907. }
  908. int av_get_bits_per_sample(enum CodecID codec_id){
  909. switch(codec_id){
  910. case CODEC_ID_ADPCM_SBPRO_2:
  911. return 2;
  912. case CODEC_ID_ADPCM_SBPRO_3:
  913. return 3;
  914. case CODEC_ID_ADPCM_SBPRO_4:
  915. case CODEC_ID_ADPCM_CT:
  916. case CODEC_ID_ADPCM_IMA_WAV:
  917. case CODEC_ID_ADPCM_MS:
  918. case CODEC_ID_ADPCM_YAMAHA:
  919. return 4;
  920. case CODEC_ID_ADPCM_G722:
  921. case CODEC_ID_PCM_ALAW:
  922. case CODEC_ID_PCM_MULAW:
  923. case CODEC_ID_PCM_S8:
  924. case CODEC_ID_PCM_U8:
  925. case CODEC_ID_PCM_ZORK:
  926. return 8;
  927. case CODEC_ID_PCM_S16BE:
  928. case CODEC_ID_PCM_S16LE:
  929. case CODEC_ID_PCM_S16LE_PLANAR:
  930. case CODEC_ID_PCM_U16BE:
  931. case CODEC_ID_PCM_U16LE:
  932. return 16;
  933. case CODEC_ID_PCM_S24DAUD:
  934. case CODEC_ID_PCM_S24BE:
  935. case CODEC_ID_PCM_S24LE:
  936. case CODEC_ID_PCM_U24BE:
  937. case CODEC_ID_PCM_U24LE:
  938. return 24;
  939. case CODEC_ID_PCM_S32BE:
  940. case CODEC_ID_PCM_S32LE:
  941. case CODEC_ID_PCM_U32BE:
  942. case CODEC_ID_PCM_U32LE:
  943. case CODEC_ID_PCM_F32BE:
  944. case CODEC_ID_PCM_F32LE:
  945. return 32;
  946. case CODEC_ID_PCM_F64BE:
  947. case CODEC_ID_PCM_F64LE:
  948. return 64;
  949. default:
  950. return 0;
  951. }
  952. }
  953. #if FF_API_OLD_SAMPLE_FMT
  954. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  955. return av_get_bits_per_sample_fmt(sample_fmt);
  956. }
  957. #endif
  958. #if !HAVE_THREADS
  959. int ff_thread_init(AVCodecContext *s){
  960. return -1;
  961. }
  962. #endif
  963. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  964. {
  965. unsigned int n = 0;
  966. while(v >= 0xff) {
  967. *s++ = 0xff;
  968. v -= 0xff;
  969. n++;
  970. }
  971. *s = v;
  972. n++;
  973. return n;
  974. }
  975. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  976. int i;
  977. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  978. return i;
  979. }
  980. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  981. {
  982. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  983. "version to the newest one from Git. If the problem still "
  984. "occurs, it means that your file has a feature which has not "
  985. "been implemented.", feature);
  986. if(want_sample)
  987. av_log_ask_for_sample(avc, NULL);
  988. else
  989. av_log(avc, AV_LOG_WARNING, "\n");
  990. }
  991. void av_log_ask_for_sample(void *avc, const char *msg)
  992. {
  993. if (msg)
  994. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  995. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  996. "of this file to ftp://upload.libav.org/incoming/ "
  997. "and contact the libav-devel mailing list.\n");
  998. }
  999. static AVHWAccel *first_hwaccel = NULL;
  1000. void av_register_hwaccel(AVHWAccel *hwaccel)
  1001. {
  1002. AVHWAccel **p = &first_hwaccel;
  1003. while (*p)
  1004. p = &(*p)->next;
  1005. *p = hwaccel;
  1006. hwaccel->next = NULL;
  1007. }
  1008. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1009. {
  1010. return hwaccel ? hwaccel->next : first_hwaccel;
  1011. }
  1012. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1013. {
  1014. AVHWAccel *hwaccel=NULL;
  1015. while((hwaccel= av_hwaccel_next(hwaccel))){
  1016. if ( hwaccel->id == codec_id
  1017. && hwaccel->pix_fmt == pix_fmt)
  1018. return hwaccel;
  1019. }
  1020. return NULL;
  1021. }
  1022. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1023. {
  1024. if (ff_lockmgr_cb) {
  1025. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1026. return -1;
  1027. }
  1028. ff_lockmgr_cb = cb;
  1029. if (ff_lockmgr_cb) {
  1030. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1031. return -1;
  1032. }
  1033. return 0;
  1034. }
  1035. unsigned int ff_toupper4(unsigned int x)
  1036. {
  1037. return toupper( x &0xFF)
  1038. + (toupper((x>>8 )&0xFF)<<8 )
  1039. + (toupper((x>>16)&0xFF)<<16)
  1040. + (toupper((x>>24)&0xFF)<<24);
  1041. }
  1042. #if !HAVE_PTHREADS
  1043. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1044. {
  1045. f->owner = avctx;
  1046. return avctx->get_buffer(avctx, f);
  1047. }
  1048. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1049. {
  1050. f->owner->release_buffer(f->owner, f);
  1051. }
  1052. void ff_thread_finish_setup(AVCodecContext *avctx)
  1053. {
  1054. }
  1055. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1056. {
  1057. }
  1058. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1059. {
  1060. }
  1061. #endif