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.

407 lines
13KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #define Y4M_MAGIC "YUV4MPEG2"
  21. #define Y4M_FRAME_MAGIC "FRAME"
  22. #define Y4M_LINE_MAX 256
  23. struct frame_attributes {
  24. int interlaced_frame;
  25. int top_field_first;
  26. };
  27. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  28. {
  29. AVStream *st;
  30. int width, height;
  31. int raten, rated, aspectn, aspectd, n;
  32. char inter;
  33. const char *colorspace = "";
  34. st = s->streams[0];
  35. width = st->codec->width;
  36. height = st->codec->height;
  37. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  38. aspectn = st->codec->sample_aspect_ratio.num;
  39. aspectd = st->codec->sample_aspect_ratio.den;
  40. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  41. inter = 'p'; /* progressive is the default */
  42. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  43. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  44. }
  45. switch(st->codec->pix_fmt) {
  46. case PIX_FMT_GRAY8:
  47. colorspace = " Cmono";
  48. break;
  49. case PIX_FMT_YUV411P:
  50. colorspace = " C411 XYSCSS=411";
  51. break;
  52. case PIX_FMT_YUV420P:
  53. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  54. break;
  55. case PIX_FMT_YUV422P:
  56. colorspace = " C422 XYSCSS=422";
  57. break;
  58. case PIX_FMT_YUV444P:
  59. colorspace = " C444 XYSCSS=444";
  60. break;
  61. }
  62. /* construct stream header, if this is the first frame */
  63. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  64. Y4M_MAGIC,
  65. width,
  66. height,
  67. raten, rated,
  68. inter,
  69. aspectn, aspectd,
  70. colorspace);
  71. return n;
  72. }
  73. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  74. {
  75. AVStream *st = s->streams[pkt->stream_index];
  76. ByteIOContext *pb = &s->pb;
  77. AVPicture *picture;
  78. int* first_pkt = s->priv_data;
  79. int width, height, h_chroma_shift, v_chroma_shift;
  80. int i, m;
  81. char buf2[Y4M_LINE_MAX+1];
  82. char buf1[20];
  83. uint8_t *ptr, *ptr1, *ptr2;
  84. picture = (AVPicture *)pkt->data;
  85. /* for the first packet we have to output the header as well */
  86. if (*first_pkt) {
  87. *first_pkt = 0;
  88. if (yuv4_generate_header(s, buf2) < 0) {
  89. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  90. return AVERROR_IO;
  91. } else {
  92. put_buffer(pb, buf2, strlen(buf2));
  93. }
  94. }
  95. /* construct frame header */
  96. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  97. put_buffer(pb, buf1, strlen(buf1));
  98. width = st->codec->width;
  99. height = st->codec->height;
  100. ptr = picture->data[0];
  101. for(i=0;i<height;i++) {
  102. put_buffer(pb, ptr, width);
  103. ptr += picture->linesize[0];
  104. }
  105. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  106. // Adjust for smaller Cb and Cr planes
  107. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  108. width >>= h_chroma_shift;
  109. height >>= v_chroma_shift;
  110. ptr1 = picture->data[1];
  111. ptr2 = picture->data[2];
  112. for(i=0;i<height;i++) { /* Cb */
  113. put_buffer(pb, ptr1, width);
  114. ptr1 += picture->linesize[1];
  115. }
  116. for(i=0;i<height;i++) { /* Cr */
  117. put_buffer(pb, ptr2, width);
  118. ptr2 += picture->linesize[2];
  119. }
  120. }
  121. put_flush_packet(pb);
  122. return 0;
  123. }
  124. static int yuv4_write_header(AVFormatContext *s)
  125. {
  126. int* first_pkt = s->priv_data;
  127. if (s->nb_streams != 1)
  128. return AVERROR_IO;
  129. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  130. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  131. }
  132. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  133. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  134. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  135. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  136. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  137. return AVERROR_IO;
  138. }
  139. *first_pkt = 1;
  140. return 0;
  141. }
  142. static int yuv4_write_trailer(AVFormatContext *s)
  143. {
  144. return 0;
  145. }
  146. #ifdef CONFIG_YUV4MPEGPIPE_MUXER
  147. AVOutputFormat yuv4mpegpipe_muxer = {
  148. "yuv4mpegpipe",
  149. "YUV4MPEG pipe format",
  150. "",
  151. "y4m",
  152. sizeof(int),
  153. CODEC_ID_NONE,
  154. CODEC_ID_RAWVIDEO,
  155. yuv4_write_header,
  156. yuv4_write_packet,
  157. yuv4_write_trailer,
  158. .flags = AVFMT_RAWPICTURE,
  159. };
  160. #endif
  161. /* Header size increased to allow room for optional flags */
  162. #define MAX_YUV4_HEADER 80
  163. #define MAX_FRAME_HEADER 80
  164. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  165. {
  166. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  167. char *tokstart,*tokend,*header_end;
  168. int i;
  169. ByteIOContext *pb = &s->pb;
  170. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  171. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  172. AVStream *st;
  173. struct frame_attributes *s1 = s->priv_data;
  174. for (i=0; i<MAX_YUV4_HEADER; i++) {
  175. header[i] = get_byte(pb);
  176. if (header[i] == '\n') {
  177. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  178. header[i+2] = 0;
  179. break;
  180. }
  181. }
  182. if (i == MAX_YUV4_HEADER) return -1;
  183. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  184. s1->interlaced_frame = 0;
  185. s1->top_field_first = 0;
  186. header_end = &header[i+1]; // Include space
  187. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  188. if (*tokstart==0x20) continue;
  189. switch (*tokstart++) {
  190. case 'W': // Width. Required.
  191. width = strtol(tokstart, &tokend, 10);
  192. tokstart=tokend;
  193. break;
  194. case 'H': // Height. Required.
  195. height = strtol(tokstart, &tokend, 10);
  196. tokstart=tokend;
  197. break;
  198. case 'C': // Color space
  199. if (strncmp("420jpeg",tokstart,7)==0)
  200. pix_fmt = PIX_FMT_YUV420P;
  201. else if (strncmp("420mpeg2",tokstart,8)==0)
  202. pix_fmt = PIX_FMT_YUV420P;
  203. else if (strncmp("420paldv", tokstart, 8)==0)
  204. pix_fmt = PIX_FMT_YUV420P;
  205. else if (strncmp("411", tokstart, 3)==0)
  206. pix_fmt = PIX_FMT_YUV411P;
  207. else if (strncmp("422", tokstart, 3)==0)
  208. pix_fmt = PIX_FMT_YUV422P;
  209. else if (strncmp("444alpha", tokstart, 8)==0) {
  210. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  211. return -1;
  212. } else if (strncmp("444", tokstart, 3)==0)
  213. pix_fmt = PIX_FMT_YUV444P;
  214. else if (strncmp("mono",tokstart, 4)==0) {
  215. pix_fmt = PIX_FMT_GRAY8;
  216. } else {
  217. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  218. return -1;
  219. }
  220. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  221. break;
  222. case 'I': // Interlace type
  223. switch (*tokstart++){
  224. case '?':
  225. break;
  226. case 'p':
  227. s1->interlaced_frame=0;
  228. break;
  229. case 't':
  230. s1->interlaced_frame=1;
  231. s1->top_field_first=1;
  232. break;
  233. case 'b':
  234. s1->interlaced_frame=1;
  235. s1->top_field_first=0;
  236. break;
  237. case 'm':
  238. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  239. return -1;
  240. default:
  241. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  242. return -1;
  243. }
  244. break;
  245. case 'F': // Frame rate
  246. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  247. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  248. break;
  249. case 'A': // Pixel aspect
  250. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  251. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  252. break;
  253. case 'X': // Vendor extensions
  254. if (strncmp("YSCSS=",tokstart,6)==0) {
  255. // Older nonstandard pixel format representation
  256. tokstart+=6;
  257. if (strncmp("420JPEG",tokstart,7)==0)
  258. alt_pix_fmt=PIX_FMT_YUV420P;
  259. else if (strncmp("420MPEG2",tokstart,8)==0)
  260. alt_pix_fmt=PIX_FMT_YUV420P;
  261. else if (strncmp("420PALDV",tokstart,8)==0)
  262. alt_pix_fmt=PIX_FMT_YUV420P;
  263. else if (strncmp("411",tokstart,3)==0)
  264. alt_pix_fmt=PIX_FMT_YUV411P;
  265. else if (strncmp("422",tokstart,3)==0)
  266. alt_pix_fmt=PIX_FMT_YUV422P;
  267. else if (strncmp("444",tokstart,3)==0)
  268. alt_pix_fmt=PIX_FMT_YUV444P;
  269. }
  270. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  271. break;
  272. }
  273. }
  274. if ((width == -1) || (height == -1)) {
  275. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  276. return -1;
  277. }
  278. if (pix_fmt == PIX_FMT_NONE) {
  279. if (alt_pix_fmt == PIX_FMT_NONE)
  280. pix_fmt = PIX_FMT_YUV420P;
  281. else
  282. pix_fmt = alt_pix_fmt;
  283. }
  284. if (raten == 0 && rated == 0) {
  285. // Frame rate unknown
  286. raten = 25;
  287. rated = 1;
  288. }
  289. if (aspectn == 0 && aspectd == 0) {
  290. // Pixel aspect unknown
  291. aspectd = 1;
  292. }
  293. st = av_new_stream(s, 0);
  294. st = s->streams[0];
  295. st->codec->width = width;
  296. st->codec->height = height;
  297. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  298. av_set_pts_info(st, 64, rated, raten);
  299. st->codec->pix_fmt = pix_fmt;
  300. st->codec->codec_type = CODEC_TYPE_VIDEO;
  301. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  302. st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  303. return 0;
  304. }
  305. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  306. {
  307. int i;
  308. char header[MAX_FRAME_HEADER+1];
  309. int packet_size, width, height;
  310. AVStream *st = s->streams[0];
  311. struct frame_attributes *s1 = s->priv_data;
  312. for (i=0; i<MAX_FRAME_HEADER; i++) {
  313. header[i] = get_byte(&s->pb);
  314. if (header[i] == '\n') {
  315. header[i+1] = 0;
  316. break;
  317. }
  318. }
  319. if (i == MAX_FRAME_HEADER) return -1;
  320. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  321. width = st->codec->width;
  322. height = st->codec->height;
  323. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  324. if (packet_size < 0)
  325. return -1;
  326. if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
  327. return AVERROR_IO;
  328. if (s->streams[0]->codec->coded_frame) {
  329. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  330. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  331. }
  332. pkt->stream_index = 0;
  333. return 0;
  334. }
  335. static int yuv4_read_close(AVFormatContext *s)
  336. {
  337. return 0;
  338. }
  339. static int yuv4_probe(AVProbeData *pd)
  340. {
  341. /* check file header */
  342. if (pd->buf_size <= sizeof(Y4M_MAGIC))
  343. return 0;
  344. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  345. return AVPROBE_SCORE_MAX;
  346. else
  347. return 0;
  348. }
  349. #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER
  350. AVInputFormat yuv4mpegpipe_demuxer = {
  351. "yuv4mpegpipe",
  352. "YUV4MPEG pipe format",
  353. sizeof(struct frame_attributes),
  354. yuv4_probe,
  355. yuv4_read_header,
  356. yuv4_read_packet,
  357. yuv4_read_close,
  358. .extensions = "y4m"
  359. };
  360. #endif