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.

205 lines
5.1KB

  1. /*
  2. * Raw Video Codec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file raw.c
  21. * Raw Video Codec
  22. */
  23. #include "avcodec.h"
  24. typedef struct PixleFormatTag {
  25. int pix_fmt;
  26. unsigned int fourcc;
  27. } PixelFormatTag;
  28. const PixelFormatTag pixelFormatTags[] = {
  29. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
  30. { -1, 0 },
  31. };
  32. static int findPixelFormat(unsigned int fourcc)
  33. {
  34. const PixelFormatTag * tags = pixelFormatTags;
  35. while (tags->pix_fmt >= 0) {
  36. if (tags->fourcc == fourcc)
  37. return tags->pix_fmt;
  38. tags++;
  39. }
  40. return PIX_FMT_YUV420P;
  41. }
  42. typedef struct RawVideoContext {
  43. unsigned char * buffer; /* block of memory for holding one frame */
  44. unsigned char * p; /* current position in buffer */
  45. int length; /* number of bytes in buffer */
  46. } RawVideoContext;
  47. static int raw_init(AVCodecContext *avctx)
  48. {
  49. RawVideoContext *context = avctx->priv_data;
  50. if (avctx->codec_tag) {
  51. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  52. }
  53. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  54. context->buffer = av_malloc(context->length);
  55. context->p = context->buffer;
  56. if (! context->buffer) {
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. static int raw_decode(AVCodecContext *avctx,
  62. void *data, int *data_size,
  63. uint8_t *buf, int buf_size)
  64. {
  65. RawVideoContext *context = avctx->priv_data;
  66. AVPicture * picture = (AVPicture *) data;
  67. /* Early out without copy if packet size == frame size */
  68. if (buf_size == context->length && context->p == context->buffer) {
  69. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  70. *data_size = sizeof(AVPicture);
  71. return buf_size;
  72. }
  73. int bytesNeeded = context->length - (context->p - context->buffer);
  74. if (buf_size < bytesNeeded) {
  75. memcpy(context->p, buf, buf_size);
  76. context->p += buf_size;
  77. *data_size = 0;
  78. return buf_size;
  79. }
  80. memcpy(context->p, buf, bytesNeeded);
  81. context->p = context->buffer;
  82. avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
  83. *data_size = sizeof(AVPicture);
  84. return bytesNeeded;
  85. }
  86. static int raw_close(AVCodecContext *avctx)
  87. {
  88. RawVideoContext *context = avctx->priv_data;
  89. av_freep(& context->buffer);
  90. return 0;
  91. }
  92. static int raw_encode(AVCodecContext *avctx,
  93. unsigned char *frame, int buf_size, void *data)
  94. {
  95. AVPicture * picture = data;
  96. unsigned char *src;
  97. unsigned char *dest = frame;
  98. int i, j;
  99. int w = avctx->width;
  100. int h = avctx->height;
  101. int size = avpicture_get_size(avctx->pix_fmt, w, h);
  102. if (size > buf_size) {
  103. return -1;
  104. }
  105. switch(avctx->pix_fmt) {
  106. case PIX_FMT_YUV420P:
  107. for(i=0;i<3;i++) {
  108. if (i == 1) {
  109. w >>= 1;
  110. h >>= 1;
  111. }
  112. src = picture->data[i];
  113. for(j=0;j<h;j++) {
  114. memcpy(dest, src, w);
  115. dest += w;
  116. src += picture->linesize[i];
  117. }
  118. }
  119. break;
  120. case PIX_FMT_YUV422P:
  121. for(i=0;i<3;i++) {
  122. if (i == 1) {
  123. w >>= 1;
  124. }
  125. src = picture->data[i];
  126. for(j=0;j<h;j++) {
  127. memcpy(dest, src, w);
  128. dest += w;
  129. src += picture->linesize[i];
  130. }
  131. }
  132. break;
  133. case PIX_FMT_YUV444P:
  134. for(i=0;i<3;i++) {
  135. src = picture->data[i];
  136. for(j=0;j<h;j++) {
  137. memcpy(dest, src, w);
  138. dest += w;
  139. src += picture->linesize[i];
  140. }
  141. }
  142. break;
  143. case PIX_FMT_YUV422:
  144. src = picture->data[0];
  145. for(j=0;j<h;j++) {
  146. memcpy(dest, src, w * 2);
  147. dest += w * 2;
  148. src += picture->linesize[0];
  149. }
  150. break;
  151. case PIX_FMT_RGB24:
  152. case PIX_FMT_BGR24:
  153. src = picture->data[0];
  154. for(j=0;j<h;j++) {
  155. memcpy(dest, src, w * 3);
  156. dest += w * 3;
  157. src += picture->linesize[0];
  158. }
  159. break;
  160. default:
  161. return -1;
  162. }
  163. return size;
  164. }
  165. AVCodec rawvideo_codec = {
  166. "rawvideo",
  167. CODEC_TYPE_VIDEO,
  168. CODEC_ID_RAWVIDEO,
  169. sizeof(RawVideoContext),
  170. raw_init,
  171. raw_encode,
  172. raw_close,
  173. raw_decode,
  174. };