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.

207 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. { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') },
  31. { -1, 0 },
  32. };
  33. static int findPixelFormat(unsigned int fourcc)
  34. {
  35. const PixelFormatTag * tags = pixelFormatTags;
  36. while (tags->pix_fmt >= 0) {
  37. if (tags->fourcc == fourcc)
  38. return tags->pix_fmt;
  39. tags++;
  40. }
  41. return PIX_FMT_YUV420P;
  42. }
  43. typedef struct RawVideoContext {
  44. unsigned char * buffer; /* block of memory for holding one frame */
  45. unsigned char * p; /* current position in buffer */
  46. int length; /* number of bytes in buffer */
  47. } RawVideoContext;
  48. static int raw_init(AVCodecContext *avctx)
  49. {
  50. RawVideoContext *context = avctx->priv_data;
  51. if (avctx->codec_tag) {
  52. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  53. }
  54. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  55. context->buffer = av_malloc(context->length);
  56. context->p = context->buffer;
  57. if (! context->buffer) {
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. static int raw_decode(AVCodecContext *avctx,
  63. void *data, int *data_size,
  64. uint8_t *buf, int buf_size)
  65. {
  66. RawVideoContext *context = avctx->priv_data;
  67. int bytesNeeded;
  68. AVPicture * picture = (AVPicture *) data;
  69. /* Early out without copy if packet size == frame size */
  70. if (buf_size == context->length && context->p == context->buffer) {
  71. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  72. *data_size = sizeof(AVPicture);
  73. return buf_size;
  74. }
  75. bytesNeeded = context->length - (context->p - context->buffer);
  76. if (buf_size < bytesNeeded) {
  77. memcpy(context->p, buf, buf_size);
  78. context->p += buf_size;
  79. *data_size = 0;
  80. return buf_size;
  81. }
  82. memcpy(context->p, buf, bytesNeeded);
  83. context->p = context->buffer;
  84. avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
  85. *data_size = sizeof(AVPicture);
  86. return bytesNeeded;
  87. }
  88. static int raw_close(AVCodecContext *avctx)
  89. {
  90. RawVideoContext *context = avctx->priv_data;
  91. av_freep(& context->buffer);
  92. return 0;
  93. }
  94. static int raw_encode(AVCodecContext *avctx,
  95. unsigned char *frame, int buf_size, void *data)
  96. {
  97. AVPicture * picture = data;
  98. unsigned char *src;
  99. unsigned char *dest = frame;
  100. int i, j;
  101. int w = avctx->width;
  102. int h = avctx->height;
  103. int size = avpicture_get_size(avctx->pix_fmt, w, h);
  104. if (size > buf_size) {
  105. return -1;
  106. }
  107. switch(avctx->pix_fmt) {
  108. case PIX_FMT_YUV420P:
  109. for(i=0;i<3;i++) {
  110. if (i == 1) {
  111. w >>= 1;
  112. h >>= 1;
  113. }
  114. src = picture->data[i];
  115. for(j=0;j<h;j++) {
  116. memcpy(dest, src, w);
  117. dest += w;
  118. src += picture->linesize[i];
  119. }
  120. }
  121. break;
  122. case PIX_FMT_YUV422P:
  123. for(i=0;i<3;i++) {
  124. if (i == 1) {
  125. w >>= 1;
  126. }
  127. src = picture->data[i];
  128. for(j=0;j<h;j++) {
  129. memcpy(dest, src, w);
  130. dest += w;
  131. src += picture->linesize[i];
  132. }
  133. }
  134. break;
  135. case PIX_FMT_YUV444P:
  136. for(i=0;i<3;i++) {
  137. src = picture->data[i];
  138. for(j=0;j<h;j++) {
  139. memcpy(dest, src, w);
  140. dest += w;
  141. src += picture->linesize[i];
  142. }
  143. }
  144. break;
  145. case PIX_FMT_YUV422:
  146. src = picture->data[0];
  147. for(j=0;j<h;j++) {
  148. memcpy(dest, src, w * 2);
  149. dest += w * 2;
  150. src += picture->linesize[0];
  151. }
  152. break;
  153. case PIX_FMT_RGB24:
  154. case PIX_FMT_BGR24:
  155. src = picture->data[0];
  156. for(j=0;j<h;j++) {
  157. memcpy(dest, src, w * 3);
  158. dest += w * 3;
  159. src += picture->linesize[0];
  160. }
  161. break;
  162. default:
  163. return -1;
  164. }
  165. return size;
  166. }
  167. AVCodec rawvideo_codec = {
  168. "rawvideo",
  169. CODEC_TYPE_VIDEO,
  170. CODEC_ID_RAWVIDEO,
  171. sizeof(RawVideoContext),
  172. raw_init,
  173. raw_encode,
  174. raw_close,
  175. raw_decode,
  176. };